Date command helps to display date in many formats. Additionally, it lets you set systems time and date.
The date command is a simplistic command which, by default, shows the current time and date of the machine. However, its functionality is not limited there.
There are a lot of ways of customizing the output of date command, by way of instance, presenting in a variety of formats, calculating dates as well as setting the system clock with admin rights!
This tool can be found on any Unix-like system. If you are running any type of Linux distro, it features the “date” tool.
This tutorial describes a few examples of how best to use date command with examples.
Syntax of date command
The “date” tool uses the following command arrangement.
date [OPTION]… [+FORMAT]
date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]
Let us look at a few examples of using the date command.
Using the simple date command example
The simple date command will show you the current date and time of the system.
$ date
Tue Oct 15 6:53:22 PST 2019
How to specify the date format
If you want the “date” command to run on a particular date, use the “-d” flag. For example,
$ date -d "<year>-<month>-<date>"
An example can be:
$ date -d 2015-6-20
Formatting date in dd-mm-yy format example
You can format the date as follows in the dd-mm-yy format:
$ date +"%d-%m-%y"
For example:
20-06-19
Output the previous date example
To print the date of the day before yesterday, run:
date --date='2 days ago'
Previous month example
$ date -d last-month
Next year example
$ date -d next-year
Next week example
$ date -d next-week
An example of a full month name
Display the current full month name and the day of the month:
$ date '+%B %d'
List of Format specifies used with date command
- %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 year (01 to 12).
- %y: Displays last two digits of the year(00 to 99).
- %Y: Screen four-digit year.
- %T: Display the time in 24 hour format as HH:MM:SS.
- %H: Display the hour.
- %M: Display the minute.
- %S: Screen the moments.
A few examples of commands with specifiers
Try these commands to see the output yourself:
$ date "+%Y-%m-%d"
Date in mm/dd/yy format command
$ date "+%D"
More
$ date "+%D %T" $ date "+%Y/%m/%d"
Try various combinations to check it yourself.
How to Check file modification time
The “date” is incredibly versatile. This tool is also used to look at the time when a particular file/folder was last modified.
date -r <file>
Overriding the time zone example
By default, whenever running date, it is going to use the current time zone. The time zone is set from the environment variable TZ. The default value of TZ is defined in the file “/etc/localtime”.
By changing the value of TZ, we could induce date to demonstrate the time of different time zones. For example.
TZ=GMT+2 date
Setting the time example
As mentioned in the introductory part, you may also set the time by using the date command.
Set Time and Date using –set option You can set time and date of your system using -s or –set option as shown below:
$ date -s "Tue Dec 17 21:00:00 PDT 2019"