Unix/Linux date command is a command used for displaying the system’s date and time. In addition, the command can also be used to modify or set the system’s date and time. For one to change the operating system’s time and date, one must be logged in as the root user. In this guide, we examine the Linux date command and how it is used.
Table of Contents
- 1 Linux Date command with no option
- 2 Display system time in Greenwich Mean-time (UTC)
- 3 Display both local and UTC time using timedatectl command
- 4 Date with –date or -d option
- 5 Using –date option to display earlier dates
- 6 Using –date option to display future dates
- 7 Setting System date and time
- 8 Display last modified timestamp of a file
- 9 Format specifiers used with date command
Linux Date command with no option
When used as it is without any options, date command displays system’s date and time as shown:
The above snippet shows the time in the EAT (East Africa Timezone) timezone (local system timezone).
Display system time in Greenwich Mean-time (UTC)
In our previous example, the system time was display in EAT timezone. To display time in UTC or Greenwich Mean Time, use the
Copy$ date -u
Display both local and UTC time using timedatectl command
Another handy command when it comes to displaying system time is the
timedatectl command. Using the command, you can display both local and UTC time as shown.
Date with –date or -d option
--date option is used to convert date from numerical to string format. For example, to convert
3/12/2019 to a string, run
Copy$ date --date=" 3/12/2019 "
Using –date option to display earlier dates
--date option can also be used to display past dates. Simply pass a string in the arguments and the date will be converted in digital form.
For example, to display the date 10 days ago, use the syntax
Copydate --date=" 10 day ago "
To display the date 5 years ago run.
Copydate --date=" 5 year ago "
To display yesterday’s date run.
Copydate --date=" yesterday "
Let’s now see how you can display future dates.
Using –date option to display future dates
Displaying future dates is easy and is similar to the previous example. You simply need to define the string of the future date in the argument.
For instance, to display tomorrow’s date run.
Copydate --date=" tomorrow "
To display next Friday’s date and time run:
Copydate --date=" next fri "
To display date and time after 2 years from the current date run:
Copydate --date=" 2 year "
Setting System date and time
Apart from displaying system date and time, date command can be used to change the date and time of your Unix/Linux system. You can achieve this using the
--set option as shown in the example below.
Copy$ date --set="date to be set"
Copy$ date --set="Mon July 8 11:54:50 EAT 2019"
Display last modified timestamp of a file
To check the last time a file was modified, use the
-r option as shown.
Copy$ date -r
Format specifiers used with date command
Before we wrap up, here is a list of format options you can use to define the output of date command.
Copy%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 the year (01 to 12). %y: Displays the 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.
The syntax of using these options is as follows.
For example to display date as mm/dd/yy run:
To display the day of the week in a string format run:
And so on and so forth.
You have come to the end of this guide. We hope it has been helpful to you and we believe that you are now comfortable using the date command. Your feedback is welcome.