the Date command is a command line utility for viewing or setting the date and time in the Linux system. It uses the system’s default time zone to display the time.
In this article, I’ll show you 12 examples of how best to use the date command on Linux. To demonstrate the following examples I used an Ubuntu 20.04 system. Since the date command is pre-integrated in all Linux systems, we do not need to install it.
$ Date [OPTION]… [+FORMAT]
Examples of date commands
By default, the date command displays the current system date and time in a standard format.
Current date of the system.
World time display
If your system time zone is based on your local time zone and you want to check the world time, we need to add the -u option to the command related to UTC to do this.
$ date -u
Custom date format
We can override the default date format with our preferred date format. To do this we need to add a format control character led by a + sign and format control starts with the% sign. Some of the most commonly used date format control characters are:
- % a – abbreviated short weekday name of the locale (e.g. Wed)
- % A – The abbreviated full weekday name of the locale (for example, Wednesday)
- % b – abbreviated short month name of the locale (e.g. Jun)
- % B – abbreviated long month name of the locale (e.g. June)
- % Y – display year (e.g. 2021)
- % m – display month (01-12)
- % d – day of the month (e.g. 02)
- % D – date as mm / dd / yy. Show
- % H – hour in 24-hour format (00-23)
- % I – hour in 12-hour format (01-12)
- % M – display minute (00-59)
- % S – display second (00-60)
- % u – day of the week (1-7)
Here in the following example we have formatted the date in the format YYYY-MM-DD.
$ date +"%Y-%m-%d"
Format the date.
$ date +"%d %b %Y"
Format the date.
Show date from string
We can view the formatted date from the user supplied date string by using the -d or -date option to the command. It has no effect on the system date, it just parses the requested date from the string. For example,
$ date -d "Feb 14 1999"
Parsing string to date.
$ date --date="09/10/1960"
Parsing string to date.
Display the upcoming date and time with the -d option
Aside from parsing the date, we can also display the upcoming date by using the -d option with the command. The date command is compatible with words related to time or date values, such as: B. next sun, last Friday, tomorrow, yesterday etc. For example:
Show the date of next Monday
$ Date -d “next Mo”
Show upcoming date.
Display of past date and time with the -d option
We can also know or display the past date using the -d option to the command. For example,
Show the date of the last Friday
$ date -d "last Fri"
Show the past date
Analyze date from file
If you have a record of the static date strings in the file, we can parse them with the -f option with the date command in the preferred date format. This allows you to format multiple dates with the command. In the example below, I created the file containing the list of date strings and parsed it using the command.
$ date -f datefile.txt
Parse the date from the file.
Set the date and time in Linux
Not only can we display the date, we can also set the system date according to your needs. To do this, you need a user with sudo access and can run the command as follows.
$ sudo date -s "Sun 30 May 2021 07:35:06 PM PDT"
Show the last modification time of the file
We can check when the file was last modified using the date command, to do this we need to add the -r option to the command. It helps in keeping track of files for when they were last modified. For example,
$ date -r /etc/hosts
Last modified date.
Overwrite the system time zone
The date command displays the date according to your configured system time zone. We need to set the TZ variable to the time zone we want to use different time zones in the area. For example, to switch to New York time, do the following:
$ TZ='America/New_York' date
Date with preferred time zone
To view all available time zones, use the timedatectl list-timezones Command.
Use the Unix epoch time
The epoch time is the number of seconds that have passed since 00:00:00 UTC on January 1, 1970. We can use the% s format control to show the number of seconds from the epoch time to the current time.
$ date +%s
Time of the Unix era.
Use date when naming files
We can create files with the current date which helps in recording the file. In the example below, I’ve created a file that has a current date in its name.
$ touch demo-$(date +"%Y-%m-%d”)
Naming file with the date.
In this article we will learn how to use the date command and how to separate send format dates on Linux.