How to Display and Set Date and Time in Linux | date Command
date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.Â
Syntax of Date Command in Linux
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Examples of How to Display and Set Date and Time in linux
1. How to Display Current Date and Time in Linux
date (no option): With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.Â
Command:
date
Output:
Display Current Date and Time in Linux
Note: Here unix system is configured in Pacific daylight time.Â
2. How to Display Time in GMT
The -u
option with the date
command allows you to display the current time in GMT (Greenwich Mean Time) or UTC (Coordinated Universal Time).Â
Command:
date -u
Output:
Display Time in GMT
–date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string.Â
Syntax:Â Â
date --date=" string "
Command:
$date --date="2/02/2010"
$date --date="Feb 2 2010"
Output:
Tue Feb 2 00:00:00 PST 2010
Tue Feb 2 00:00:00 PST 2010
4. How to Display Past Dates
Date and time of 2 years ago
date --date="2 year ago"
Date and time of 2 years ago.Â
Date and time of 5 seconds ago
date --date="5 sec ago"
Date and time of 5 seconds ago
Date and time of previous day
date --date="yesterday"
Date and time of previous day
Date and time of 2 months ago
date --date="2 month ago"
Date and time of 2 months ago
Date and time of 10 days ago
date –date=”10 day ago”
Date and time of 10 days ago
5. How to Display Future Dates
Date and time of upcoming particular week day. Â
Command:
$date --date="next tue"
Output:
Tue Oct 17 00:00:00 PDT 2017
Date and time after two days.Â
Command:
$date --date="2 day"
Output:
Fri Oct 13 00:05:52 PDT 2017
Date and time of next day.Â
Command:
$date --date="tomorrow"
Output:
Thu Oct 12 00:08:47 PDT 2017
Date and time after 1 year on the current day.Â
Command:
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2018
6. How to Set the System Date and Time
Use the -s
or --set
option to set the system date and time to a specific value.
Syntax:Â Â
date --set="date to be set"
Command:
$date
Output:
Wed Oct 11 15:23:26 PDT 2017
Command:
$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date
Output:
Tue Nov 13 15:23:34 PDT 2018
7. How to Display Date Strings from a File
–file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line.Â
Syntax:Â
date --file=file.txt
$cat >> datefile
Sep 23 2018
Nov 03 2019
Command:
$date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov 3 00:00:00 PDT 2019
8. How to Display Last Modified Timestamp of a File
The -r
option is used to display the last modified timestamp of a specified file.
Syntax:Â Â
date -r file.txt
We can modify the timestamp of a datefile by using touch command. Â
$touch datefile
$date -r datefile
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date -r datefile
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile
%D: Display date as mm/dd/yy.
%d: Display the day of the month (01 to 31).
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.
Syntax:Â Â
date +%[format-option]
Examples:Â Â
1. Display Date in MM/DD/YY Format
date "+%D"
Display Date in MM/DD/YY Format
2. Display Date and Time in MM/DD/YY HH:MM:SS Format
date "+%D %T"
Display Date and Time in MM/DD/YY HH:MM:SS Format
3. Display Date in YYYY-MM-DD Format
date "+%Y-%m-%d"
Display Date in YYYY-MM-DD Format
4. Display Date in YYYY/MM/DD Format
date "+%Y/%m/%d"
Display Date in YYYY/MM/DD Format
5. Display Full Date and Time with Day, Month, Date, Time, and Year
date "+%A %B %d %T %y"
Display Full Date and Time with Day, Month, Date, Time, and Year
Conclusion
In this article we discussed the ‘date’ command in Linux which is a versatile tool for managing system date and time. It allows users to display the current date, customize output formats, set system dates, and navigate past or future dates. Whether showcasing time in GMT, adjusting temporal settings, or using format specifiers, ‘date’ is a powerful command for effective system administration, enabling users to efficiently handle various time-related tasks in the Linux environment.
Last Updated :
06 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...