How to use date command in Linux
date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.
In this tutorial, we learn about the date command in Linux with usage examples.
How to use the Linux date command?
The following is the syntax of date command on the Linux terminal:
$ date [OPTION]... [+FORMAT] $ date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
By default the date command prints the date in the timezone in which the system is configured. To change the date and time you should need a user with root or sudo privilege.
1. Display Date
When the date command is used without any options and arguments, it displays the current date and time in the default format as follows:
The output includes the day of the week, month, day of the month, time, timezone, and year:
~$ date Mon 08 Nov 2021 07:06:49 AM CET
2. Show Future dates
The date string
--date option allows you to print the future or upcoming dates. To print the future dates, type the values into the strings such as “
next Friday“, “
tomorrow” or similar…
~$ date -d "tomorrow" Tue 09 Nov 2021 07:11:32 AM CET ~$ date -d "next Friday" Fri 12 Nov 2021 12:00:00 AM CET
3. Display past dates
-d option, you can display the past dates on your Linux system.
- Date and time of 2 years ago.
~$ date --date="2 year ago" Fri 08 Nov 2019 07:15:42 AM CET
- Date and time of 5 seconds ago.
$ date --date="5 sec ago" Mon 08 Nov 2021 07:18:07 AM CET
- Date and time of 10 days ago.
~$ date --date="10 day ago" Fri 29 Oct 2021 08:19:22 AM CEST
4. Date Command Format 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"
~$ date +"Year: %Y, Month: %m, Day: %d" Year: 2021, Month: 11, Day: 08
- %a – Prints weekday’s name in short format (e.g., Mon)
- %A – Used to display full weekday name (e.g., Monday)
- %b – Display the name of the month in short form (e.g., Jan)
- %B – Used to display the full month’s name (e.g., January)
- %d – Displays the month’s day (e.g., 05)
- %H – Display Hour (00..23)
- %I – Display Hour in (01..12) format
- %j – Displays the Day of year (001..366)
- %m – Displays Month in number (01..12)
- %M – Print Minutes in 00..59 sec.
- %S – Displays seconds (00..60)
- %u – Display week’s day in number (1..7)
- %Y – Used to display Full-year (e.g., 2019)
Using the following command, you can explore the full list of format options of the date command:
$ date --help $ man date
5. Display Date from a String Value
-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 "2021-11-08 12:10:32"
~$ date -d "2021-11-08 12:10:32" Mon 08 Nov 2021 12:10:32 PM CET
-d option, you can also display the date in the custom formatting as follows:
~$ date -d '08 Oct 2021' +'%A, %d %B %Y' Friday, 08 October 2021
6. Date command to set 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:
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.
7. 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 1636353817
For example, to view how many seconds elapsed from an epoch to a specific date, use the following command:
~$ date -d "1984-02-10" +"%s" 445215600
8. 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 02 Nov 2021 11:58:15 PM CET
9. 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, to manually set the date and time of the Linux system to 5:30 PM, November 08, 2021. Run the below-mentioned command:
$ date --set="20211108 05:30"
The Linux date command displays or set the system date and time.To explore more about the date command please visit the date command man pages.
If you have any questions or feedback, feel free to leave a comment.