How to use date command.

How to use date command.

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

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.

 

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 and arguments:

date

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

Wed May  3 12:22:32 EEST 2023

 

Date Formatting Options

The output of the date command 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: 2023, Month: 05, Day: 03

Here is another example:

date "+DATE: %D%nTIME: %T"
DATE: 05/03/23
TIME: 13:21:13

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)

  • %s – 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 accepts values such as “tomorrow”, “friday”, “last friday” “next friday”, “next month”, “next week” ..etc.

date -d "last week"
Wed Apr 26 13:21:47 EEST 2023

You can also use the date string option to show the 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 May  7 23:30:00 EEST 2023

 

Override the Timezone

The date command returns the date in 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, invoke date with the %s format control:

date +%s
1683109370

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

date -d @1234567890
Sat Feb 14 01:31:30 EET 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

 

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
Wed Jan 25 20:40:03 EET 2023

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 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

The Linux date command displays or set the system date and time.