Complete the Linux date command. No, it can't bring you a romantic evening. However, it can format the date at the top of a love letter you wrote in the terminal. Near enough? Let's begin.
When scripting in Bash, you inevitably have to print a date or time, and that date or time often needs to be in a specific format to meet the needs of other functions. Then the date command comes into play.
As you will see, the date command on Linux is both simple and versatile, meaning it accepts all types of input and generates dates in a variety of formats. It also has other special functions for various time-related computing tasks. Either way, as you learn the options and syntax of the date, you will become more familiar with scripts and possibly more punctual.
Basic syntax of the date command
The basic syntax for the date command is as follows:
Date (OPTION) … (+ FORMAT)
That is, after entering dateyou can enter an option such as -d or -sto invoke a specific function that we will explain below.
You can also follow these formatting strings that always start with a + Character. These strings use certain formatting characters, also listed below, to define the output.
Practical examples of Linux date commands
You can use the date command in a number of ways. Let's consider the most common and useful use cases of the same.
1. Get the current date and time
You can get the current local date and time in standard format by passing the date command yourself.
Monday, April 19, 2021, 12:41:17 p.m. CDT
As you can see, Date gives you the relevant date and time information in a simple and predictable format.
2. Get a past or future date
Suppose you need to calculate the time and date in your script, which is exactly a week from now. You are protected with the date command. Issue this command with the command -d Option to get detailed information on future dates:
$ date -d "next week"
Tue 27 Apr 2021 17:21:07 CDT
The -d Option, short for date, is where the date command really shines. Various custom date strings are accepted. You can be technical, like 20200315, 03/15/20or readable like March 15, 2020. But you can also use relative terms such as tomorrow, yesterday, next Sunday, and more. Play around with it and see how the date interprets various input strings.
3. Format a date
You may have noticed in the previous two examples that the date has a very specific time format by default. What if you need it in a different format?
You can format your output similar to the printf command. For example, you can use this command to print the current year:
Date + "Year:% Y"
The + signals that you want a formatted string and whatever appears after that in the Quote Markings and dates are processed and formatted for output.
Here is a list of the formatting characters you are most likely to use:
|% H.||Hour (00-24)|
|% M.||Minute (00-59)|
|% S.||Second (00-60)|
|% p||Am or PM|
|%A||Full name of the day of the week (e.g. Sunday)|
|%a||Day of the week abbreviated name (e.g. Sun)|
|% w||Weekday number (0-6)|
|% d||Day of the month (01-31)|
|% j||Day of the year (001-366)|
|% B.||Full name of the month (e.g. January)|
|% b||Month abbreviated name (e.g. Jan)|
|% m||Month number (01-12)|
A full list of formatting characters is provided with –Help Option in the terminal.
Date – help
4. Get the day of the week
A very common and convenient use of date formatting is to get the day of the week for a specific date. For example, to find out which day of the week fell on November 4th, 1995, enter a similar command:
$ date -d "1996-04-11" + "% A"
The -d Option indicates that you want a specific date that "1996-04-11" String indicates what date you want and that + "% A" The formatting indicates that the day of the week should be displayed in the output. Remember that the date string can come in many formats, not just the one given here.
5. Get the coordinated universal time
By issuing the -u With this flag you can get the current time in Coordinated Universal Time (UTC).
$ date -u
Wed 21 Apr 2021 12:46:59 UTC
6. Enter the local time in a different time zone
If you need a date in a different time zone, you can do so by setting TZ = Environment variable before the date command.
For example, you can view the current date and time in Mountain Standard Time (MST) with the following command:
$ TZ = MST date
Tue 20 Apr 2021 3:45:29 PM MST
Just replace for your purposes MST with the initials for each time zone you prefer. You can also use UTC notation. For example, replace to get the same time zone MST With UTC + 7.
In addition, you can name a continent and a major city to determine the local time of that particular city. For example:
$ TZ = date America / Phoenix
Tue 20 Apr 2021 3:45:29 PM MST
7. Get the last modification time of a file
For example, when making backups, you often need to get the last modified date of a file. You can do this by passing the -r Option and naming of a file.
$ date -r / etc / shadow
Wed 14 Apr 2021 07:53:02 CDT
You can also change the timestamps of a file on Linux using the touch command.
8. Output and convert epoch time
You can calculate the number of seconds since the Unix epoch with the following command:
$ date +% s
You can also reverse the process and convert the Unix time to a human readable format using the feature -d Option and @ Character.
$ date -d @ 1618955631
Tue 20 Apr 2021 4:53:51 PM CDT
Calculating Unix time is useful when you need an accurate second that other devices will definitely sync.
9. Set the system time temporarily
You can change your system clock from the terminal with the date command by passing the key -s Argument followed by the desired time. For example, you can set the system clock to 24 hours in the future with the following command:
Date -s "tomorrow"
Note that you need sudo permissions to pass this command. Additionally, the change is unlikely to be permanent (meaning your clock will revert to the previous time after a reboot) as most distros use other utilities to manage your system clock which will overwrite the change on startup.
Linux date command explained
As in life, you can't get out of time with Linux. For this reason, it is important that you understand how to format and use it through the date command. One thing that you are sure to come across in Linux file management is the various timestamps that files carry with them.
Understanding timestamps for Linux files: mtime, ctime, and atime
Want to learn more about how Linux tracks changes in a file? Here's what you need to know about Linux file timestamps.
About the author
(38 articles published)
Jordan is a tutor and journalist who is passionate about making Linux accessible and stress-free for everyone. He has a BA in English and a thing for hot tea. During the warm season he enjoys cycling on the hills of the Ozarks where he lives.
By Jordan Gloor
Subscribe to our newsletter
Sign up for our newsletter to receive tech tips, reviews, free e-books, and exclusive offers!
One more step …!
Please confirm your email address in the email we just sent you.