Date Command in Linux is very extensive and dynamic, provides very rich date formatting and is greatly customizable for working with scripts which depend on time based invocations.
date command can also be used to set the system date and it requires the root permission.
Lets run date command and examine the output.
[vamshi@node02 log]$ date Wed Apr 1 13:52:21 UTC 2020
Now lets examine some of the most useful options that comes with the date command.
date command along with
--set option can take for following format to set the new system time and date.
How to set the system date in Linux using date command?
[vamshi@node02 log]$ sudo date -s 'Apr 01 2020 13:52:59 UTC' Wed Apr 1 13:52:59 UTC 2020
The date can also be setup in shot hand notation as follows,but it is more cryptic
[vamshi@node02 log]$ sudo date 040113522020.50 Wed Apr 1 13:52:50 UTC 2020
$ sudo date
The format is month of the Year(mm),day of the month(dd),Hour of the day(HH),minute of the Hour(MM) and the Year(yyyy),and the Seconds of the minute(.SS)
Now, Lets dive deep and get to know the date options and Demonstration practical examples in this tutorial:
Another Important Option is -d or –date=”String” which can display the time described
Lets see some examples as follows:
By running the date command, we get an elaborate time and date format along with the TimeZone information.
To covert the Epoc time to human readable date, we can use date command as follows:
[vamshi@node02 log]$ date -d"@1585749164" Wed Apr 1 13:52:44 UTC 2020
If you want to get a future date then use:
[vamshi@linuxcent ~]$ date -d "+130 days" Sun Aug 16 02:07:35 UTC 2020
Date command offers a great flexibility to extract past and future dates as we will show below:
$ date "+ %F" -d “+30 days”
$ date "+ %F" --date “+30 days”
To get the date in history; go back to a date some days ago in Linux
[vamshi@node02 log]$ date -d "17 days ago" Sun Mar 15 13:52:45 UTC 2020
Here we present some of the more useful Format options:
|Date Format Command||Explanation||Result|
|date +”%a”||Prints the Abbreviated Day of the Week Sat-Sun||Wed|
|date +”%A”||Prints the Day of the Week Saturday-Sunday||Wednesday|
|date +”%b”||Prints Abbreviated Month Jan-Dec||Apr|
|date +”%B”||Print un-abbreviated month January-December||April|
|date +”%c”||Prints Full Current Date and time format||Wed Apr 1 13:52:43 UTC 2020|
|date +”%D”||Prints dd/mm/yy date format||04/01/2020|
|date +”%d”||Prints day of the month (01-31)||01|
|date +”%D”||Prints Date in MM/DD/YY||04/01/20|
|date +”%e”||Prints the Day of the month||01|
|date +”%F”||Prints only the Full date as YYYY-MM-DD||2020-04-01|
|date +”%H”||Prints the hour 00-23||13|
|date +”%I”||Prints the hour in 00-12||01|
|date +”%j”||Prints Julian day of the Year(001-366)||092|
|date +”%M”||Prints the Minute of the hour 00-59||52|
|date +”%m”||Prints the month of the year 01-12||04|
|date +”%n”||Prints the newline character||Newline/Empty line|
|date +”%N”||Prints the nanoseconds counts||036416306|
|date +”%P”||Prints AM/PM in the day||PM|
|date +”%r”||Get only time in AM/PM notation||13:52:43 PM|
|date +”%S”||Get the current seconds count in the minute (00-60)||43|
|date +”%s”||Get the number of seconds since 1st January 1970 (Epoch time)||1585749164|
|date +”%T”||Time in 24 Hour format HH:MM:YY||13:52:43|
|date +”%u”||Get current day of the week
|3 for Wednesday|
|date +”%U”||Get the current week of the Year considering Sunday as first week||13|
|date +”%V”||Get the current week of the Year considering Monday as first week||14|
|date +”%y” or date +”%g”||Prints only the last two digits of Year||20|
|Date +“%Y” or date +”%F”||Prints Year in YYYY format||2020|
|Date +“%z”||Prints the current Timezone difference from UTC||00 – for UTC|
|date +”%Z”||Prints Alphabetic time zone abbreviation||UTC|
How to write the current system time to the Machine’s hardware clock ?
hwclock can do that for us.
[/code] # sudo hwclock [OPTIONS][/code]
Lets see a practical example where our
Hardware clock was 1 hour and 13 mins behind the actual system time .
[vamshi@node02 ~]$ sudo hwclock Wed 01 Apr 2020 07:35:05 AM UTC -0.454139 seconds
[vamshi@node02 ~]$ date Wed Apr 01 08:43:13 UTC 2020
Setting the hardware clock time to system time with option
--systohc as seen below.
[vamshi@node02 ~]$ sudo hwclock -w
Confirm it with
hwclock command as follows:
[vamshi@node02 ~]$ sudo hwclock Wed 01 Apr 2020 08:44:05 AM UTC -0.538163 seconds
Most of the times the hardware clock will be out of sync with the system time and its a good practice to set the hardware clock in sync and comes in real handy during the system reboots.