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 [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.
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:
%a– short weekday name (e.g., Mon)
%A– full weekday name (e.g., Monday)
%b– short month name (e.g., Jan)
%B– 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)
Get the full list of all formatting options, you would run
date --help or
man date in your terminal.
-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
For example, to show the
New York, America time, you would type:
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.
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:
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
-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.
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.