Dates can be printed in a variety of formats. We can also set dates from the command line. In UNIX-like systems, dates are stored as an integer in seconds since 1970-01-01 00:00:00 UTC. This is called epoch or UNIX time. Let’s see how to read dates and set them.
Display Date And Time
You can read the date as follows:
date
The epoch time can be printed as follows:
date +%s
Epoch is defined as the number of seconds that have elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.
We can find out epoch from a given formatted date string. You can use dates in multiple date formats as input.
date --date "Mon Feb 3 11:09:53 UTC 2020" +%s
The –date option is used to provide a date string as input.
The date format strings are listed in the following table:
Date component | Format |
Weekday | %a (for example:. Mon)
%A (for example: Monday) |
Month | %b (for example: Feb)
%B (for example: February) |
Day | %d (for example: 03) |
Date in format (mm/dd/yy) | %D (for example: 02/03/20) |
Year | %y (for example: 20)
%Y (for example: 2020) |
Hour | %I or %H (for example: 11) |
Minute | %M (for example: 16) |
Second | %S (for example: 33) |
Nano second | %N (for example: 684287968) |
epoch UNIX time in seconds | %s (for example: 1580728610) |
Use a combination of format strings prefixed with + as an argument for the date command to print the date in the format of your choice.
date "+%d %B %Y"
Set Date And Time
We can set the date and time as follows:
date -s "15 June 2021 18:01:25"