Read in 4 minutes

last updated 

Linux Date Command with Examples

The date command displays or sets the system date. It is most commonly used to print the date and time in different formats and calculate future and past dates.

In this tutorial, we’ll cover the basics of the date command.

Using the Linux date Command

The syntax for the date command is as follows:

date [OPTION]... [+FORMAT]

To display the current system time and date using the default formatting, invoke the command without any options:

date

The output will include the day of the week, month, day of the month, time, timezone, and year:

Sat Jun  1 14:31:01 CEST 2019

Date Formatting Options

The output of date can be formatted with a sequence of format control characters preceded by a + sign. The format controls start with the % symbol and are substituted by their values.

date +"Year: %Y, Month: %m, Day: %d"

The %Y character will be replaced with the year, %m with month and %d with the day of the month:

Year: 2019, Month: 06, Day: 02

Here is another example:

date "+DATE: %D%nTIME: %T"
DATE: 06/02/19
TIME: 01:47:04

Below is a small list of the some of the most common formatting characters:

  • %a - Locale’s abbreviated short weekday name (e.g. Mon)
  • %A - Locale’s abbreviated full weekday name (e.g. Monday)
  • %b - Locale’s abbreviated short month name (e.g. Jan)
  • %B - Locale’s abbreviated long month name (e.g. January)
  • %d - Day of month (e.g., 01)
  • %H - Hour (00..23)
  • %I - Hour (01..12)
  • %j - Day of year (001..366)
  • %m - Month (01..12)
  • %M - Minute (00..59)
  • %S - Second (00..60)
  • %u - Day of week (1..7)
  • %Y - Full year (e.g. 2019)

To get a full list of all formatting options run date --help or man date in your terminal.

Date String

The -d option allows you to operate on a specific date. You can specify the date as a human-readable date string like below:

date -d "2010-02-07 12:10:53"
Sun Feb  7 12:10:53 CET 2010

Using the custom formatting:

date -d '16 Dec 1974' +'%A, %d %B %Y'
Monday, 16 December 1974

The date string can also accept values such as “tomorrow”, “friday”, “last friday” “next friday”, “next month”, “next week” ..etc.

date -d "last week"
Sat May 25 14:31:42 CEST 2019

You can also use the date string option to show your local time for different timezones. For example, to show the local time for 6:30AM next Monday on the Australian east coast, you would type:

date -d 'TZ="Australia/Sydney" 06:30 next Monday'
Sun Jun  2 22:30:00 CEST 2019

Override the Timezone

The date command uses the default system timezone. To use a different timezone set the environment variable TZ to the desired timezone.

For example, to show the Melbourne, Aus time, you would type:

TZ='Australia/Melbourne' date
Sat Jun  1 22:35:10 AEST 2019

To list all available time zones, you can either list the files in the /usr/share/zoneinfo directory or use the timedatectl list-timezones command.

Epoch Converter

The date command can be used as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have elapsed since January 1, 1970 at 00:00:00 UTC.

To print the number of the seconds from the epoch to the current day use the %s format control:

date +%s
1559393792

To convert seconds since the epoch to date, set the seconds as a date string prefixed with @:

date -d @1234567890
Sat Feb 14 00:31:30 CET 2009

Using date with Other Commands

The date command is most frequently used to create filenames that contain the current time and date.

The command below will create a Mysql backup file in the following format database_name-20190601.sql

mysqldump  database_name > database_name-$(date +%Y%m%d).sql

You can also use the date command in your shell scripts. In the example below we are assigning the output of date to the date_now variable:

date_now=$(date "+%F-%H-%M-%S")

echo $date_now
2019-06-01-15-02-27

Display the Last Modification Time of a File

The date command with the -r option shows the last modification time of a file. For example:

date -r /etc/hosts
Tue Jul 24 11:11:48 CEST 2018

If you want to modify the file timestamp, use the touch command.

Set the System Time and Date

Setting the system time and date manually with the date command is usually not recommended because on most Linux distributions the system clock is synchronized using the ntp or the systemd-timesyncd services.

However, if you want to set the system clock manually, you can use the --set= option. For example, if you want to set the date and time to 5:30pm, June 01, 2019, you would type:

date --set="20190601 17:30"

Conclusion

By now you should have a good understanding of how to use the Linux date command.

If you have any question or feedback, feel free to leave a comment.