Intro To 'date' Command In Linux
2023-05-15 - By Robert Elder
I use the 'date' command to print the current date:
date
Fri 12 May 2023 12:14:05 PM EDT
Custom Time Zones With The 'date' Command
By default, the 'date' command will print the current date in the current time zone. You can include the '--utc' flag to see the date printed in coordinated universal time:
date --utc
Fri 12 May 2023 04:14:18 PM UTC
Or, you can explicitly specify a time zone in the 'TZ' shell variable like this:
TZ='America/Los_Angeles' date
Fri 12 May 2023 09:14:41 AM PDT
Custom Format Strings With The 'date' Command
I can also use a custom date format string to print out the date in any way that I want like this:
date +"%Y-%m-%d %H:%M:%S"
2023-05-12 12:17:15
or like this:
date +"%Y___%m____%d %H--%M------%S"
2023___05____12 12--17------31
Relative Dates With The 'date' Command
The 'date' command will print the date from 10 days in the past using this command:
date --date="10 days ago"
Tue 02 May 2023 12:23:38 PM EDT
Using The 'date' Command With Unix Timestamp
The 'date' command also lets you specify a date using a Unix timestamp by placing an '@' symbol before the number like this:
date --date="@1677476463"
Mon 27 Feb 2023 12:41:03 AM EST
or like this:
date --date="@1"
Wed 31 Dec 1969 07:00:01 PM EST
date --date="@1" --utc
Thu 01 Jan 1970 12:00:01 AM UTC
More 'date' Command Format Specifier Documentation
The 'date' command supports many other flags and format strings that can be found in the 'man' pages:
man date
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available.
or also the 'info' pages:
info date
21.1.1 Time conversion specifiers
---------------------------------
‘date’ conversion specifiers related to times.
‘%H’
hour (‘00’...‘23’)
‘%I’
hour (‘01’...‘12’)
‘%k’
hour, space padded (‘ 0’...‘23’); equivalent to ‘%_H’. This is a
GNU extension.
‘%l’
hour, space padded (‘ 1’...‘12’); equivalent to ‘%_I’. This is a
GNU extension.
‘%M’
minute (‘00’...‘59’)
‘%N’
nanoseconds (‘000000000’...‘999999999’). This is a GNU extension.
‘%p’
locale’s equivalent of either ‘AM’ or ‘PM’; blank in many locales.
Noon is treated as ‘PM’ and midnight as ‘AM’.
‘%P’
like ‘%p’, except lower case. This is a GNU extension.
‘%r’
locale’s 12-hour clock time (e.g., ‘11:11:04 PM’)
‘%R’
24-hour hour and minute. Same as ‘%H:%M’.
‘%s’
seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC. Leap
seconds are not counted unless leap second support is available.
*Note %s-examples::, for examples. This is a GNU extension.
‘%S’
second (‘00’...‘60’). This may be ‘60’ if leap seconds are
supported.
‘%T’
24-hour hour, minute, and second. Same as ‘%H:%M:%S’.
‘%X’
locale’s time representation (e.g., ‘23:13:48’)
‘%z’
Four-digit numeric time zone, e.g., ‘-0600’ or ‘+0530’, or ‘-0000’
if no time zone is determinable. This value reflects the numeric
time zone appropriate for the current time, using the time zone
rules specified by the ‘TZ’ environment variable. A time zone is
not determinable if its numeric offset is zero and its abbreviation
begins with ‘-’. The time (and optionally, the time zone rules)
can be overridden by the ‘--date’ option.
‘%:z’
Numeric time zone with ‘:’, e.g., ‘-06:00’ or ‘+05:30’), or
‘-00:00’ if no time zone is determinable. This is a GNU extension.
‘%::z’
Numeric time zone to the nearest second with ‘:’ (e.g., ‘-06:00:00’
or ‘+05:30:00’), or ‘-00:00:00’ if no time zone is determinable.
This is a GNU extension.
‘%:::z’
Numeric time zone with ‘:’ using the minimum necessary precision
(e.g., ‘-06’, ‘+05:30’, or ‘-04:56:02’), or ‘-00’ if no time zone
is determinable. This is a GNU extension.
‘%Z’
alphabetic time zone abbreviation (e.g., ‘EDT’), or nothing if no
time zone is determinable. See ‘%z’ for how it is determined.
21.1.2 Date conversion specifiers
---------------------------------
‘date’ conversion specifiers related to dates.
‘%a’
locale’s abbreviated weekday name (e.g., ‘Sun’)
‘%A’
locale’s full weekday name, variable length (e.g., ‘Sunday’)
‘%b’
locale’s abbreviated month name (e.g., ‘Jan’)
‘%B’
locale’s full month name, variable length (e.g., ‘January’)
‘%c’
locale’s date and time (e.g., ‘Thu Mar 3 23:05:25 2005’)
‘%C’
century. This is like ‘%Y’, except the last two digits are
omitted. For example, it is ‘20’ if ‘%Y’ is ‘2000’, and is ‘-0’ if
‘%Y’ is ‘-001’. It is normally at least two characters, but it may
be more.
‘%d’
day of month (e.g., ‘01’)
‘%D’
date; same as ‘%m/%d/%y’
‘%e’
day of month, space padded; same as ‘%_d’
‘%F’
full date in ISO 8601 format; same as ‘%Y-%m-%d’. This is a good
choice for a date format, as it is standard and is easy to sort in
the usual case where years are in the range 0000...9999.
‘%g’
year corresponding to the ISO week number, but without the century
(range ‘00’ through ‘99’). This has the same format and value as
‘%y’, except that if the ISO week number (see ‘%V’) belongs to the
previous or next year, that year is used instead.
‘%G’
year corresponding to the ISO week number. This has the same
format and value as ‘%Y’, except that if the ISO week number (see
‘%V’) belongs to the previous or next year, that year is used
instead. It is normally useful only if ‘%V’ is also used; for
example, the format ‘%G-%m-%d’ is probably a mistake, since it
combines the ISO week number year with the conventional month and
day.
‘%h’
same as ‘%b’
‘%j’
day of year (‘001’...‘366’)
‘%m’
month (‘01’...‘12’)
‘%q’
quarter of year (‘1’...‘4’)
‘%u’
day of week (‘1’...‘7’) with ‘1’ corresponding to Monday
‘%U’
week number of year, with Sunday as the first day of the week
(‘00’...‘53’). Days in a new year preceding the first Sunday are
in week zero.
‘%V’
ISO week number, that is, the week number of year, with Monday as
the first day of the week (‘01’...‘53’). If the week containing
January 1 has four or more days in the new year, then it is
considered week 1; otherwise, it is week 53 of the previous year,
and the next week is week 1. (See the ISO 8601 standard.)
‘%w’
day of week (‘0’...‘6’) with 0 corresponding to Sunday
‘%W’
week number of year, with Monday as first day of week
(‘00’...‘53’). Days in a new year preceding the first Monday are
in week zero.
‘%x’
locale’s date representation (e.g., ‘12/31/99’)
‘%y’
last two digits of year (‘00’...‘99’)
‘%Y’
year. This is normally at least four characters, but it may be
more. Year ‘0000’ precedes year ‘0001’, and year ‘-001’ precedes
year ‘0000’.
Consult the actual 'info' pages for even more format specifiers.
And that's why the 'date' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|