Signup/Sign In
PUBLISHED ON: FEBRUARY 16, 2023

date Command in Linux with Examples

The "date" command on Linux is used to display or set the system date and time. It can be used in a variety of ways to display the date and time in different formats, or to set the date and time manually. The command can also be used to synchronize the system clock with a remote server using the Network Time Protocol (NTP). This is a useful tool for maintaining accurate time on a Linux system, which is important for various tasks such as logging and scheduling.

Syntax of date command

date [CHOICE]... [+FORMAT]
date [-u|—utc|—universal] [MMDDhhmm[[CC]YY] [.ss]]

Examples of date Command in Linux

1. date (no option) :

The date command displays the current date and time, as well as the abbreviated day name, abbreviated month name, day of the month, time separated by colons, time zone name, and year, if no variables are specified.

Command:

$date

Output:

1

Note: Here Unix system is configured in pacific daylight time.

2. date -u :

The "date -u" command in Linux is used to display the current date and time in Coordinated Universal Time (UTC) format. UTC is a standard time reference that is used globally and is not affected by daylight saving time. This command is useful for applications that require accurate timekeeping and synchronization, such as network and distributed systems.

command :

$date -u 

Output :

2

3. –date or -d Option:

displays the specified date string in date format. However, this has no effect on the system's real date and time value. Rather, it employs the date and time specified as a string.

Syntax:

$date --date=" string "

Output:

3

4. Using the -- date option to view prior dates:

$date --date="string"
  • Date and time of 5 years ago.
  • Date and time of 6 years ago.
  • Date and time of 2 months ago.
  • Date and time of 10 months ago.

4

5. Using the –date option to display the future:

  • Date and time of upcoming particular weekday.
  • Date and time after two days.
  • Date and time after 2 years.

5

6. s or - set Option:

To set the system date and time, use the -s or –set switch.
Syntax:

$date --set="date to be set"

6

7: –file or -f :

This option displays the date string on each line of the file in the date and time format. This option is identical to the –date option, with the exception that several date strings can be specified on each line of a file, whereas –date only accepts a single date string.

Syntax:

$date --file=file.txt

7

8. -r Option:

This option displays the last modification time of a data file.
Syntax:

$date -r file.txt

With the touch command, the timestamp of a datefile may be modified.

$access datefile

8

9. List of Format specifiers used with date command:

%D: Format the date as mm/dd /yy.
%d: Display the current month's day (01 to 31).
%a: Displays the shortened weekday names (Sun to Sat).
%A: Displays complete weekdays (Sunday to Saturday).
%h: Displays the month's shortened name (Jan to Dec).
%b: Displays the month's shortened name (Jan to Dec).
%B: Displays the complete month name (January to December).
%m: Indicates the month of the year (01 to 12).
%y: Displays the year's last two digits (00 to 99).
%Y: Display a four-digit year value.
%T: Display the time as HH:MM:SS in 24-hour format.
%H: Display the current hour.
%M: Display the current minute.
%S: Display the number of seconds.

Syntax:

$date +%[format-option]

9

Conclusion

The "date" command in Linux is a useful tool for displaying and manipulating the current system date and time. It allows users to view the current date and time, change the date and time, and format the output to suit their needs. This command is essential for managing and synchronizing system clocks and for logging and tracking purposes. Overall, the "date" command is a powerful and essential tool for Linux users.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.