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
- 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
5. Set Date and Time using -s or -set option
You can set the date and time of your system using the -s or -set option as shown below.
In this example, initially, it displayed the time as 20:09:31. We then used the date command to change it to 21:00:00.
date
sudo date -s 'Mon june 1 00:00:00 EDT 2024'
date
Subscribe to my newsletter
Read articles from Mahir Dasare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by