DEV Community

mahir dasare
mahir dasare

Posted on

Linux “date” Command

Introduction:
Date command is helpful to display the date in several formats. It also allows you to set
systems date and time.
This article explains a few examples of how to use date commands with practical examples.
When you execute the date command without any option, it will display the current date and
time as shown below.
date

Read Date Patterns from a file using -file option

This is similar to the -d or -date option that we discussed above. But, you can do it for
multiple date strings. If you have a file that contains various static date strings, you can use -
f or -file option as shown below.
In this example, we can see that datefile contained 2 date strings. Each line of datefile is
parsed by date command and date is outputted for each time.
echo 12/2/2020 >> datefile
echo Feb 7 2020 10:10:10 >> datefile

2) Get Relative Date Using -date option

For example, the following examples get the date of next Monday.
date
date --date="next mon"
It displays the date in which 5 seconds are elapsed since epoch 1970-01-01 UTC:
date --date=@5
It displays the date in which 10 seconds are elapsed since epoch 1970-01-01 UTC:
date --date=@10
It displays the date in which 1 minute (i.e 60 seconds) is elapsed since epoch 1970-01-01
UTC:
date --date=@6

3) Display Past Date.

You can display a past date using the -date command. Few possibilities are shown below.

date
date --date='30 seconds ago'
date --date='1 day ago'
date --date='yesterday'
date --date='1 month ago'

4) Display Universal Time using -u option_**

You can display the date in UTC format using -u, or -utc, or -universal option as shown
below.
*date -u
Image description
Image description*

Top comments (0)