Linux Date

Laxman Naik
4 min readOct 10, 2021

Hello everyone!πŸ™‹β€β™‚οΈπŸ™‹β€β™‚οΈπŸ™‹β€β™‚οΈ

This article explains few examples on how to use date command with practical examples.

What is 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 linux operating system is configured. You must be the super-user (root) to change the date and time. There are various options associated with Date command, few of them are listed below:

When you execute date command without any option, it will display the current date and time as shown below.

[root@localhost ~]# date
Sun Oct 10 22:08:25 IST 2021

To read the complete manual of Date, we use:

[root@localhost ~]# man date

Display date from a string value:

β€” date option is used to take input date as string value and print it in standard format as shown below. Please note that it doesn’t print the current date and time instead it print the given date and time in standard format. If you don’t specify time, it uses 00:00:00 for time.

[root@localhost ~]# date --date="02/11/2020"
Tue Feb 11 00:00:00 IST 2020
[root@localhost ~]# date --date="11 feb 2020"
Tue Feb 11 00:00:00 IST 2020
[root@localhost ~]# date --date="feb 14 2020"
Fri Feb 14 00:00:00 IST 2020
[root@localhost ~]# date --date="aug 14 2020 13:14:15"
Fri Aug 14 13:14:15 IST 2020

Read date from a file:

This method is similar to above but in this case you can display multiple date string present in a file. If you have a file that contains various static date strings, you can use -file option as shown below.

[root@localhost ~]# cat datefile
sept 29 1987
aug 23 1999
feb 17 2021 13:15:23
[root@localhost ~]# date --file=datefile
Tue Sep 29 00:00:00 IST 1987
Mon Aug 23 00:00:00 IST 1999
Wed Feb 17 13:15:23 IST 2021

Relative date:

You can use date command to get a future date using relative values.

The below example get the date of next Monday.

[root@localhost ~]# date --date="next mon"
Mon Oct 11 00:00:00 IST 2021

If string=@is given to date command, then date command convert seconds since the epoch (1970–01–01 UTC) to a date.

It displays date in which 5 seconds are elapsed since epoch 1970–01–01 UTC:

[root@localhost ~]# date --date=@5
Thu Jan 1 05:30:05 IST 1970

It displays date in which 10 seconds are elapsed since epoch 1970–01–01 UTC:

[root@localhost ~]# date --date=@10
Thu Jan 1 05:30:10 IST 1970

It displays date in which 1 minute (i.e. 60 seconds) is elapsed since epoch 1970–01–01 UTC:

[root@localhost ~]# date --date=@60
Thu Jan 1 05:31:00 IST 1970

Display past date:

One can display past dates using -date command. Few examples are shown below:

[root@localhost ~]# date --date="10 seconds ago"
Sun Oct 10 23:08:46 IST 2021
[root@localhost ~]# date --date="yesterday"
Sat Oct 9 23:09:07 IST 2021
[root@localhost ~]# date --date="1 day ago"
Sat Oct 9 23:09:21 IST 2021
[root@localhost ~]# date --date="1 month ago"
Fri Sep 10 23:09:32 IST 2021
[root@localhost ~]# date --date="1 year ago"
Sat Oct 10 23:09:46 IST 2020
[root@localhost ~]# date --date="10 year ago"
Mon Oct 10 23:09:54 IST 2011
[root@localhost ~]# date --date="25 month ago"
Tue Sep 10 23:10:08 IST 2019
[root@localhost ~]# date --date="256 day ago"
Wed Jan 27 23:10:19 IST 2021
[root@localhost ~]# date --date="25 year ago"
Thu Oct 10 23:10:35 IST 1996

Set date and Time:

You can set date and time of system using -set option as shown below:

In this example, initially it displayed the time as 23:20:22 and day as Sun Oct 10. We then used date command to change it to 00:00:00 and Mon Oct 11.

[root@localhost ~]# date
Sun Oct 10 23:20:22 IST 2021
[root@localhost ~]# date -s"Mon Oct 11 00:00:00 IST 2021"
Mon Oct 11 00:00:00 IST 2021
[root@localhost ~]# date
Mon Oct 11 00:00:03 IST 2021

Display Universal Time:

You can display universal date in UTC format using -u option as shown below:

[root@localhost ~]# date
Sun Oct 10 23:28:29 IST 2021
[root@localhost ~]# date -u
Sun Oct 10 17:58:33 UTC 2021

Various Date Command Formats:

You can use formatting option to display date command in various formats using the following syntax:

[root@localhost ~]# $ date +%<format-option>

The following displays various date command formatting options.

[root@localhost ~]# date +%a
Sun
[root@localhost ~]# date +%A
Sunday
[root@localhost ~]# date +%b
Oct
[root@localhost ~]# date +%B
October
[root@localhost ~]# date +%d
10
[root@localhost ~]# date +%D
10/10/21
[root@localhost ~]# date +%F
2021-10-10
[root@localhost ~]# date +%H
23
[root@localhost ~]# date +%I
11
[root@localhost ~]# date +%j
283
[root@localhost ~]# date +%m
10
[root@localhost ~]# date +%M
34
[root@localhost ~]# date +%S
17
[root@localhost ~]# date +%N
626561354
[root@localhost ~]# date +%T
23:34:29
[root@localhost ~]# date +%u
7
[root@localhost ~]# date +%U
41
[root@localhost ~]# date +%Y
2021
[root@localhost ~]# date +%Z
IST

I hope you enjoyed this article

Thank you…..

--

--