date Command in Linux



The date command in Unix primarily displays or sets the date and time. It fetches the date and time from the kernel clock, and modifying the date and time also updates the kernel clock.

Additionally, the date command is used to get past and future dates, day of the week, date and time in another timezone, the last modification time of a file, and Epoch time.

Note − This guide explains the GNU version of the date command. Some commands may not be compatible with macOS. To use the GNU date command on macOS, install coreutils using brew.

Table of Contents

Syntax for date Command

The syntax of using the date command is mentioned below −

date [options]… [+format]

The date command comes with options to set the date and time and format the output. Replace the [options] with the date command options and [+format] with the format specifiers.

If it is executed without any option, the currently configured system date and time will be displayed. Note that to modify the date and time, the user needs sudo privileges.

Options for date Command

Options or flags of a command change the behavior and enhance the usability of a command. Various options for the date command are listed in the table below −

Flags Options Description
-d --date='String' To print the date and time mentioned in the form of a string
-f --file=DateFile To read the date string in a file
-r --reference=File To print the last modification date and time of a file
-s --set='NewDate' To change the date and time
-u --utc, --universal To print the date in Coordinated Universal Time
-I[Format] --iso-8601=[format] To print the date and time in the ISO 8601 format (e.g., 2024-06-10, 13:30:00); use [format] for additional arguments: hours, minutes, seconds
-R --rfc-email

To print the date and time in RFC 5322 format

(e.g., 10-Jun-2024, 01:30:00 +0500)

--help To display the help about the date command usage

Format Control Specifier for date Command

To display the date and time in a specific format, format specifiers are employed just like printf command. The format specifiers are also termed modifiers.

The commonly used specifiers with the date command are listed below −

Specifier Description
%a To print abbreviated weekday names (Mon, Tue)
%A To print the complete weekday name (Monday, Tuesday)
%b To print abbreviated month names (Jan, Feb)
%B To print the complete month name (January, February)
%d To print the day of the month
%D To print date in %d/%m/%y format
%F To print the full date in yyyy-mm-dd format
%g To print the last two digits of the year (24)
%G To print all four digits of the year (2024)
%j To print the day of the year in numeric form (001-366)
%r To print only time in 12-hr format
%R To print only time in 24-hr format
%s To print seconds since EPOCH
%T To print time in %h:%m:%s format
%u To print the day of the week in numeric form (1-7)
%U To print the week of the year in numeric, Sunday is the first day of the week

Using date Command

In this section, we will learn different ways to use the date command in Unix/Linux-like operating systems.

Get the Current Date and Time

To display the current date and time in the default format, execute the date command without any option.

date
date Command Linux 1

Print a Specific Date or Time

To display a specific date instead of the default date, the -d flag or --date option is used. The example is as follows −

date -d "2024/06/11" 

Or −

date --date="2024/06/11"
date Command Linux 2

Print the Past and Future Dates

The date command can also print the past and future dates. For example, to print yesterday's date, use −

date --date="yesterday"
date Command Linux 3

Or −

date --date="1 day ago"
date Command Linux 4

To display the date and time of the last Monday −

date --date="last Monday"
date Command Linux 5

To get the future date −

date --date="tomorrow"
date Command Linux 6

And for more future dates −

date --date="2 months"
date --date="next week"
date Command Linux 7

Format the Date

The date command output can be formatted using modifiers followed by the + sign.

To get the day of the week −

date +"The day of the week is %A"

To print the month name to the standard output −

date +"The month is %B"

To get the full date and time −

date +"Date: %F and Time: %T"
date Command Linux 8

Get the Day of Week of a Specific Date

To get the day of the week of a specific date −

date -d "2 month ago" +%A
date Command Linux 9

The -d flag is fetching a date of two months ago and the %A specifier is getting the day of the week of that specific date.

Get Coordinated Universal Time

The Coordinated Universal Time or UTC is the time standard by which time around the world is regulated.

To get the time and date in UTC, use the -u flag.

date -u
date Command Linux 10

Print Date String from a File

To print the date or time from each line of a file, the -f or --file option with the filename is used.

date -f "myfile.txt"
date Command Linux 11

Print the Last Modified Date and Time of a File

To extract the last modification date of a file, the date command with the -r flag or --reference option can be employed. The example is as follows −

date -r "/home/user/myfile.txt"
date Command Linux 12

Set Date and Time

To set the date and time the -s flag or --set option is used. Let's understand it with an example −

sudo date -s "2024-06-10 09:30:00"
date Command Linux 13

The output shows the current date and time and then the new date and time after executing the command.

Note − In the above command, sudo is used because changing the date and time requires superuser privileges.

Get Local Time in another Timezone

To print the local time in another timezone is another example showing the significance of the date command.

First list all the time zones, and note the timezone in which you want to display the time and date −

timedatectl list-timezones

Now, assign the required timezone to the TZ environment variable and use the date command −

TZ=America/Chicago date
date Command Linux 14

Get Unix Epoch Time

The %s modifier is used with the date command to get the Unix timestamp or Unix Epoch time. Use the following command to get the time passed since January 1, 1970.

date +%s
date Command Linux 15

Let's calculate the number of seconds passed from Epoch time to a specific date and time −

date -d "2023-01-01 18:30:00" +%s

To get a human-readable form of date and time, use −

date -d @[epoch-time]
date Command Linux 16

Advanced Examples of date Command

The date is a handy command that has various use cases, especially in bash scripting.

Using date Command for Data Logging

For example, time and date are crucial components of data logging. The following script monitors the system memory and logs to a file with timestamps.

#!/bin/bash

while true
do
   Mem=$(free -m | grep -i 'mem\|used')
   Time=$(date +"%r")
   echo  -e "Memory Usage:\n $Mem | At $Time" >> file.txt
   sleep 5
done

In the above script, the date command with %r specifier fetches the current time from the system.

date Command Linux 17

The output shows that every 5 seconds the memory usage is being logged to file.txt, thanks to the date command.

Using date Command for Data Backup

The date can also be a crucial command in backing up data. The following bash script backs up a directory with the date and timestamps to the backup folder.

#!/bin/bash

directory=/home/user/backup

while true
do
   echo "Backing up!"
   tar czf $directory/backup_$(date +'%Y_%m_%d_%H_%M_%S').tar.gz --absolute-names /home/user/dir
   sleep 10
done

In the above script, the date command inserts the date and time of the back to the file name.

date Command Linux 18

Conclusion

The date is an important command-line utility and comes pre-installed in Linux and Unix-like operating systems. It primarily prints the current date and time to the standard output. Moreover, using flags and options, its functionality can further be enhanced for complex use cases. To format the output the format modifiers options are used.

Advertisements