12 Useful Linux date Command Examples

The date command is a command-line utility for displaying or setting date and time in the Linux system. It uses the system default time zone to display the time.

In this article, I will show you 12 examples of how to best use the date command on Linux. To demonstrate the examples below I have used an Ubuntu 20.04 system. As the date command is pre-integrated in all Linux systems we don’t need to install it.

Syntax :

$ date [OPTION]... [+FORMAT]

Date Command Examples

Displaying Date

By default, the date command will display the current system date and time in a default format.

$ date

Display current date

Current date of the system.

Displaying Universal Time

If your system time zone is based on your local time zone and you want to check the universal time, to do so we need to add the -u option to the command which refers to UTC.

$ date -u

Display UTC time and date

UTC.

Custom Date Format

We can overwrite the default date format with our preferred date format. To achieve that we need to add a format control character led by + sign and format control begins with the % sign. Some of the most used date format control characters are:

  • %a - Locale’s abbreviated short weekday name (e.g., Wed)
  • %A - Locale’s abbreviated full weekday name (e.g., Wednesday)
  • %b - Locale’s abbreviated short month name (e.g., Jun)
  • %B - Locale’s abbreviated long month name (e.g., June)
  • %Y - Display Year (e.g., 2021)
  • %m - Display Month (01-12)
  • %d - Day of month (e.g., 02)
  • %D - Display date as mm/dd/yy
  • %H - Hour in 24 hr format (00-23)
  • %I - Hour in 12 hr 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 formatted the date in yyyy-MM-dd format.

$ date +"%Y-%m-%d"

Use a custom date format

Formatting date.

Similarly,

$ date +"%d %b %Y"

Custm date format without time

Formatting date.

Displaying Date From String

We can display the formatted date from the date string provided by the user using the -d or --date option to the command. It will not affect the system date, it only parses the requested date from the string. For example,

$ date -d "Feb 14 1999"

Pass a string containing a date to date command

Parsing string to date.

$ date --date="09/10/1960"

Parsing string to date

Parsing string to date.

Displaying Upcoming Date & Time With -d Option

Aside from parsing the date, we can also display the upcoming date using the -d option with the command. The date command is compatible with words that refer to time or date values such as next Sun, last Friday, tomorrow, yesterday, etc. For examples,

Displaying Next Monday Date

$ date -d "next Mon"

Display futire dates and date calculations

Displaying upcoming date.

Displaying Past Date & Time With -d Option

Using the -d option to the command we can also know or view past date. For examples,

Displaying Last Friday Date

$ date -d "last Fri"

Displaying past date

Parse Date From File

If you have a record of the static date strings in the file we can parse them in the preferred date format using the -f option with the date command. In this way, you can format multiple dates using the command. In the following example, I have created the file that contains the list of date strings and parsed it with the command.

$ date -f datefile.txt

Parse multiples dates from file

Parse date from the file.

Setting Date & Time on Linux

We can not only view the date but also set the system date according to your preference. For this, you need a user with Sudo access and you can execute the command in the following way.

$ sudo date -s "Sun 30 May 2021 07:35:06 PM PDT"

Display File Last Modification Time

We can check the file’s last modification time using the date command, for this we need to add the -r option to the command. It helps in tracking files when it was last modified. For example,

$ date -r /etc/hosts

Display file modification time

Last modified date.

Override the System Timezone

The date command will display the date according to your configured system time zone. We need to set the TZ variable to the desired time zone to use various time zones in the environment. For example, to switch to New York time, execute:

$ TZ='America/New_York' date

Use TZ variable to override time zone

Date with prefer time zone

To see all available time zones, use the timedatectl list-timezones command.

Use Unix Epoch Time

Epoch time is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC. We can use %s format control to view the number of seconds from epoch time to current time.

$ date +%s

Seconds since since January 1, 1970, at 00:00:00 UTC

Unix epoch time.

Using Date in File Naming

We can create files with the current date which helps in keeping the track record of the file. In the following example, I have created a file including a current date in its name.

$ touch demo-$(date +"%Y-%m-%d”)

Use date in file names

Naming file with the date.

Conclusion

In this article, we learn how to use the date command and how to pare send format dates on Linux.