Introduction
The date
command shows or changes the current system date. It's most typically used to print the date and time in various formats, as well as to calculate future and previous dates.
We'll go through the basics of the date
command in this tutorial. We will also address a few FAQs on Date command in Linux.
Using the Linux date
Command
The date command has the following syntax:
date [OPTION]... [+FORMAT]
Invoke the command without any options or arguments to display the current system time and date in the default format:
date
The weekday, month, day of the month, time, timezone, and year are all included in the output:
Sat Apr 18 14:31:01 CEST 2022
Date Formatting Options
A sequence of format control characters preceded by a +
sign can be used to format the date
command's output. The %
symbol precedes the format controls, which are then replaced by their values.
date +"Year: %Y, Month: %m, Day: %d"
The year will be replaced by the %Y
character, the month by the %m
character, and the day of the month by the %d
character:
Output
Year: 2022, Month: 04, Day: 18
Another example:
Output
date "+DATE: %D%nTIME: %T"
Output
DATE: 06/02/19
TIME: 01:47:04
Here's a quick rundown of some of the most commonly used formatting characters:
%a
- Locale’s abbreviated short weekday name (e.g., Tue)%A
- Locale’s abbreviated full weekday name (e.g., Tuesday)%b
- Locale’s abbreviated short month name (e.g., Oct)%B
- Locale’s abbreviated long month name (e.g., October)%d
- Day of month (e.g., 02)%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., 2022)
Run date --help
or man date
in your terminal to see a complete list of all formatting choices.
Date String
You can use the -d
option to operate on a certain date. The date can be specified as a human-readable date string, as shown below:
date -d "2010-02-07 12:10:53"
Output
Sun Feb 7 12:10:53 CET 2010
Using the custom formatting:
date -d '16 Dec 1974' +'%A, %d %B %Y'
Output
Monday, 16 December 1974
The date string might be anything from "tomorrow" to "friday", "last friday", "next friday", "next month", "next week", and so on.
date -d "last week"
Output
Sat May 25 14:31:42 CEST 2019
The date string option can also be used to show the time in several timezones. For instance, to display the local time for 6:30 a.m. on Monday on Australia's east coast, type:
date -d 'TZ="Australia/Sydney" 06:30 next Monday'
Sun Apr 18 22:30:00 CEST 2022
Override the Timezone
The date
command returns the current date in the system's default timezone. Set the environment variable TZ to the desired timezone to utilize a different timezone.
To see the time in Melbourne, Australia, for example, type:
TZ='Australia/Melbourne' date
Mon Apr 18 22:35:10 AEST 2022
You can either list the files in the /usr/share/zoneinfo
directory or use the timedatectl list-timezones
command to see all possible time zones.
Epoch Converter
The date
command can be used to convert between Epochs. The epoch, often known as Unix timestamps, is the number of seconds since January 1, 1970, at 00:00:00 UTC.
Invoke date
with the %s
format control to print the number of seconds from the epoch to the current day:
date +%s
Output
1559393792
Set the seconds as a date string prefixed with @:
to convert seconds since the epoch to date.
date -d @1234567890
Output
Sat Feb 14 00:31:30 CET 2009
Using date
with Other Commands
The date
command is typically used to create filenames that include 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
The date
command can also be used in shell scripts. The output of date
is assigned to the date_now
variable in the example below:
date_now=$(date "+%F-%H-%M-%S")
echo $date_now
Output
2019-06-01-15-02-27
Display the Last Modification Time of a File
The date
command with the -r
option displays the date and time of a file's last modification. Consider the following scenario:
date -r /etc/hosts
Output
Tue Jul 24 11:11:48 CEST 2018
The touch
command can be used to change the file timestamp.
Set the System Time and Date
Because most Linux distributions use the ntp
or systemd-timesyncd
services to synchronize the system clock, manually setting the system time and date
with the date command is not advised.
If you want to manually set the system clock, you can use the --set=
option. For instance, if you wish to set the date and time for April 18, 2022, at 5:30 p.m., you would type:
date --set="20220418 17:30"
FAQs for Date Command in Linux
How do I use the date
command to display the current date and time?
To display the current date and time, simply enter date
in the command line, and it will show the output in the default format.
Can I use the date
command to display the date and time in a specific format?
Yes, you can use various formatting options with the date
command to display the date and time in a specific format. For example, date +"%Y-%m-%d %H:%M:%S"
will give you the output in the "YYYY-MM-DD HH:MM:SS" format.
How can I set the system date and time using the date
command?
To set the system date and time, you need superuser privileges. Use the command sudo date -s "YYYY-MM-DD HH:MM:SS"
with the desired date and time values replaced accordingly.
Can I display the date and time in a different timezone using the date
command?
Yes, you can display the date and time in a different timezone by setting the TZ
environment variable. For example, TZ="America/New_York" date
will display the date and time in the Eastern Time Zone.
Can I calculate dates and times using the date
command?
Yes, you can perform date and time calculations with the date
command. Use the -d
option followed by a date or time expression. For example, date -d "tomorrow"
will display the date of the next day.
How can I convert a timestamp to a human-readable date using the date
command?
You can convert a timestamp to a human-readable date using the -d @timestamp
option with the date
command. Replace timestamp
with the actual timestamp value.
Can I customize the output format of the date
command?
Yes, you can customize the output format of the date
command by using formatting options, such as +%format
after the date
command. Replace format
with the desired format string.
Conclusion
In conclusion, the date
command in Linux is a versatile tool for working with dates and times. It allows you to display the current date and time, set the system date and time, format the output in different ways, perform calculations, and work with timezones and locales.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.