Operations 9 min read

Master the Linux date Command: Formats, Options, and Real‑World Examples

Learn how to use the Linux date command to display, format, and manipulate dates and times, including options for custom strings, file input, relative dates, setting system time, UTC display, and retrieving file modification timestamps, with practical code examples and a comprehensive format table.

Raymond Ops
Raymond Ops
Raymond Ops
Master the Linux date Command: Formats, Options, and Real‑World Examples

Date is a versatile Linux command that can display the current date and time in many formats and also set the system clock.

<code>$ date
Mon May 20 22:02:24 PDT 2013</code>

1. Use the –date option to display a date from a string

When you provide a static date or time string, the -d or –date option parses the string and displays it, ignoring the current system time.

If you omit the time, 00:00:00 is used.

<code>$ date --date="12/2/2014"
Tue Dec  2 00:00:00 PST 2014

$ date --date="2 Feb 2014"
Sun Feb  2 00:00:00 PST 2014

$ date --date="Feb 2 2014"
Sun Feb  2 00:00:00 PST 2014</code>

You can also include a time component in the string:

<code>$ date --date="Feb 2 2014 13:12:10"
Sun Feb  2 13:12:10 PST 2014</code>

2. Use the –file option to read date patterns from a file

The -f/–file option works like –date but processes each line of a file as a separate date string.

<code>$ cat datefile
Sept 9 1986
Aug 23 1987

$ date --file=datefile
Tue Sep  9 00:00:00 PDT 1986
Sun Aug 23 00:00:00 PDT 1987</code>

3. Use –date for relative dates

You can obtain future or past dates by specifying relative expressions.

<code>$ date --date="next mon"
Mon May 27 00:00:00 PDT 2013</code>

Using the @ syntax, you can convert seconds since the Unix epoch to a date:

<code>$ date --date=@5
Wed Dec 31 16:00:05 PST 1969

$ date --date=@10
Wed Dec 31 16:00:10 PST 1969

$ date --date=@60
Wed Dec 31 16:01:00 PST 1969</code>

4. Display past dates

The –date option also accepts expressions like "3 seconds ago", "1 day ago", "yesterday", "1 month ago", or "1 year ago" to show past dates.

<code>$ date --date='3 seconds ago'
Mon May 20 21:59:20 PDT 2013

$ date --date='1 day ago'
Sun May 19 21:59:36 PDT 2013

$ date --date='yesterday'
Sun May 19 22:00:26 PDT 2013

$ date --date='1 month ago'
Sat Apr 20 21:59:58 PDT 2013

$ date --date='1 year ago'
Sun May 20 00:09:09 PDT 2012</code>

5. Use –set to change the system date and time

The -s or –set option lets you set the system clock.

<code>$ date
Sun May 20 09:31:00 PDT 2013

$ date -s "Sun May 20 21:00:00 PDT 2013"
Sun May 20 21:00:00 PDT 2013

$ date
Sun May 20 21:00:05 PDT 2013</code>

6. Use -u to display UTC

The -u, -utc, or -universal options show the date in Coordinated Universal Time.

<code>$ date
Mon May 20 07:53:00 PDT 2013

$ date -u
Tue May 21 07:55:00 UTC 2013</code>

7. Use -r to show a file's last modification time

After modifying a file's timestamp with touch, -r displays that timestamp.

<code>$ date
Sun May 20 25:48:00 PDT 2013

$ touch datefile

$ date
Sun May 20 26:12:00 PDT 2013

$ date -r datefile
Sun May 20 25:57:00 PDT 2013</code>

8. Various date format options

You can customize the output using format specifiers with the + option, such as %a for abbreviated weekday, %A for full weekday, %b for abbreviated month, %B for full month, %d for day of month, %D for MM/DD/YY, %F for YYYY‑MM‑DD, %H for 24‑hour, %I for 12‑hour, %j for day of year, %m for month number, %M for minute, %S for second, %N for nanoseconds, %T for HH:MM:SS, %u for ISO weekday number, %U for week number, %Y for full year, and %Z for time‑zone abbreviation.

Linuxshellsysadmindate formattingdate command
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.