Site icon DesignLinux

Date Command in Linux

Date Command in Linux

In Linux, the date command is used to get or set the system date. It is used to print the date and time in different formats and calculate future and past dates. This tutorial explains the basics of the date command.

Using the Linux date Command

Following is the basic syntax for the date command:

date [OPTION]... [+FORMAT]

If you run the date command without any options or arguments it will show the current system time and date in default formatting.

date
Mon Oct 5 13:25:53 UTC 2020

In output, it will show the day of the week, month, day of the month, time, timezone, and year:

Date Formatting Options

You can format the output of the date command 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"
Year: 2020, Month: 10, Day: 05

As you can see in output the %Y replaced with the year, %m with month and %d with the day of the month.

Let’s see second example:

date "+DATE: %D%nTIME: %T"
DATE: 10/05/20
TIME: 13:27:34

Following is the list of most common formatting characters:

Get the full list of all formatting options, you would run date --help or man date in your terminal.

Date String

Using the -d option you can operate a specific date. Use the date. You can specify the date as a human-readable date string like below:

date -d "2020-08-05 11:08:53"
Wed Aug 5 11:08:53 UTC 2020

You also can do custom formatting like below:

date -d '12 Dec 2016' +'%A, %d %B %Y'
Monday, 12 December 2016

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

date -d "last friday"
Fri Oct 2 00:00:00 UTC 2020

Override the Timezone

By default the date command returns the date in the system timezone. If you want to get timezone in different timezone set the environment variable TMZN for the specific timezone.

For example, to show the New York, America time, you would type:

TMZN='America/New_York' date
Mon Oct 5 13:48:52 UTC 2020

You can get the list of all available time zones from 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 display the number of the seconds from the epoch to the current day, run date with the %s format control:

date +%s
1601906837

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

date -d @1601906837
Mon Oct 5 14:07:17 UTC 2020

Display the Last Modification Time of a File

Use the -r option along with the date command to get the last modification time of a file.

date -r /etc/passwd
Tue Aug 4 06:29:00 UTC 2020

You can modify the file timestamp using the touch command.

Conclusion

You learned how to use the date command to set or get the system date and time.

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

Exit mobile version