As a part of the Linux basic commands series, in this post, we will learn one of the very basic and frequently used commands – date.
You may not have a Linux flavored system for practicing Linux commands. Do not worry as you can use any online Linux terminals. Refer to this post to choose one.
I am using Webminal. Register yourself and access a Linux terminal for learning.
Please note Linux commands are case sensitive and always use lower case for commands. “date” and “Date” are not the same. The usage of the “date” command is as below:-
“OPTION” and “FORMAT” both are optional. Some of mostly used options are as below:-
–date=STRING -> displays time described by STRING
–set=STRING -> Set time described by STRING
To know the current date with timestamp
“date” command gives you the current time with the day of the week followed by the date, time with timezone, and year. I am in IST Timezone but the Linux online editor which I am using is configured in CET timezone. This is the reason we see CET not IST.
To know which timezone you are
You need to use a format %Z with a date. To use any format with “date” command we need to use + sign.
To change timezone
TZ is an environment variable that specifies the timezone. If you want to set IST timezone then use a command as shown below:-
To reset the timezone
use “reset TZ” command.
We can directly print time for different timezone without setting and unsetting as below:-
To get previous and next date
You need to use English words “tomorrow” and “yesterday” with an option “–date” to get dates as below.
Similarly, you need to use other English words to get a date after or previous year/s, month/s, or day/s, second/s, etc as shown below.
To use past dates add “ago” and for future dates do not add any extra word. If we do not add the word “ago” it means you are looking for a future date. “10 second” and “10 seconds” both are fine.
To set a time
To set time use below command. Since I am using an online Linux editor where I am not allowed to set time.
To print date in specific formats
There are many formats provided by date command. You can get all available formats by using “date –h” command. I will give some examples below:-
To get current month full name
To get only current day
To get date in a specific format say 30-Oct-20
Please not here that to use multiple formats of date, do not use + sign for each format.
To get full and abbreviated weekday name
You can always use “date –h” command to learn more about it.
You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
Many other topics you can navigate through the menu.