Unless otherwise specified,
date normally pads numeric fields
with zeros, so that, for
example, numeric months are always output as two digits.
Seconds since the epoch are not padded, though,
since there is no natural width for them.
The following optional flags can appear after the ‘
0’. In addition, precede any year number with ‘
+’ if it exceeds 9999 or if its field width exceeds 4; similarly, precede any century number with ‘
+’ if it exceeds 99 or if its field width exceeds 2. This supports ISO 8601 formats for dates far in the future; for example, the command
date --date=12019-02-25 +%+13Foutputs the string ‘
Here are some examples of padding:
date +%d/%m -d "Feb 1" ⇒ 01/02 date +%-d/%-m -d "Feb 1" ⇒ 1/2 date +%_d/%_m -d "Feb 1" ⇒ 1/ 2
You can optionally specify the field width
(after any flag, if present) as a decimal number. If the natural size of the
output of the field has less than the specified number of characters,
the result is written right adjusted and padded to the given
size. For example, ‘
%9B’ prints the right adjusted month name in
a field of width 9.
An optional modifier can follow the optional flag and width specification. The modifiers are:
%y’ and ‘
%Y’ conversion specifiers. In a Japanese locale, for example, ‘
%Ex’ might yield a date format based on the Japanese Emperors’ reigns.
If the format supports the modifier but no alternate representation is available, it is ignored.
POSIX specifies the behavior of flags and field widths only for
%G’, and ‘
%Y’ (all without
modifiers), and requires a flag to be present if and only if a field
width is also present. Other combinations of flags, field widths and
modifiers are GNU extensions.