7 Examples of Date Command in Linux
The date command gives you the current date and time of you Linux system. But it can do a lot more than that. Check out these practical examples.
The date command in Linux is the simplest way to display the current time and date in Linux.
But, there’s more to it than meets the eye.
Yes, you can do a couple more things using the date command like identifying a week or day from a date, calculating the time from epoch to a specified time, and a few more variations.’
In this article, we take a look at the date command in detail and what you can do with it using some examples.
Linux date command
Here’s the syntax of the date command:
date [option] [+format]
Here the option is essentially a flag like -d, – debug, etc which I’ll discuss in the later section of this article.
And, the format lets you control the output by adding your own string and specify the values you want in the output.
I have included some examples below which should give you some clarity on what I explained here.
1. Display the current date and time in Linux
To start with, you just need to enter the command “date” to display the date and time. Here’s how it looks with the input and output:
ankushdas@linuxhandbook:~$ date Mon 02 Dec 2019 03:02:33 PM IST
As you can see, it also shows the time zone along with the system time.
Suppose, you have a specific date but you want to display the weekday for that date, you need to specify the date using the -d flag as:
date -d "1996-08-25"
It is worth noting that the format for the input is YYYY-MM-DD. So, if you type something in a different order, you will encounter an error that should say this:
ankushdas@linuxhandbook:~$ date -d "25-08-1996" date: invalid date ‘25-08-1996’
So, when you get an invalid date error while using the date command, simply check the date format of your input.
For this, you get an output that should look like:
ankushdas@linuxhandbook:~$ date -d "1996-08-25" Sun 25 Aug 1996 12:00:00 AM IST
In addition to these use-cases, you can also control the output and use the formatting options available. Let us take a quick peek at the available formatting options.
2. Format date command output
Just like I mentioned above, you can control what you want in the output using the date command.
if you want to just display the current time in Linux, use this:
Another example of the command to show the control over output format:
date +"Weekday: %A Month: %B"
In the command above, I added a string after + inside the inverted commas and then pass some pre-built sequences like %A or %B to control what you want to display.
When you do this, expect a similar output like this:
ankushdas@linuxhandbook:~$ date +"Week: %V Year: %y" Week: 49 Year: 19
You shall find a list of sequences similar in addition to all the information associated with the date command when you type in this:
This is how it should look like when you scroll down through the instructions after the help command:
3. Use date command to display a time/date for past/future
Suppose you want to know what the date was last week, you can do that using the date command.
All you have to do is type in:
date -d last-week
Similarly, you can use “tomorrow” / “last-year” / “next-year” / “next-month” and so on.
4. Use date command to identify a specific day
If you have a particular date but want to identify the day of the week, you can do that easily by typing in:
date -d "1996-08-25" +"%A"
The output for this when you type it in should look like:
linuxhandbook@pop-os:~$ date -d "1996-08-25" +"%A" Sunday
Here, %A helps to specify the date, you can change it to %B to display the month name as well. And, the right after the flag -d, you type the date you want to identify.
5. Calculate the time from epoch time
Another interesting use of the date command is calculating the time in seconds since epoch time or Unix time.
In other words, you will be able to know how many seconds have elapsed since 1 January 1970 00:00:00 UTC to the present time.
To do that, simply type in the following command in the terminal:
At the time of writing this, I got the output as follows:
ankushdas@linuxhandbook:~$ date +%s 1575282601
This may not be something useful for everyone – but it is an interesting use-case for date command in Linux.
You can also try specifying a point of time since the epoch time to calculate the number of seconds elapsed.
For instance, if you want to calculate the number of seconds elapsed up until January 2000, type in the following:
date -d "2000-01-01" +"%s"
Here’s how it looks like in the terminal:
ankushdas@linuxhandbook:~$ date -d "2000-01-01" +"%s" 946665000
6. Use date command to set the timezone
While installing the Linux distro, you already have your timezone set. You can also you can change it from the settings using the GUI.
However, if you want to just see the time for a timezone without changing your system settings, you can use the date command.
All you need to type is:
You will be shown the time in that specific zone as:
ankushdas@linuxhandbook:~$ TZ=PST date Mon 02 Dec 2019 11:07:25 AM PST
7. Change the system date from the terminal
You may not need to change your system date from the terminal (you can always the GUI).
However, if you want to utilize the date command through the terminal, you can do it by typing the following command:
date --set="201901202 22:00"
When you type this command, it will set the time to December 2nd, 2019 at 12:00 PM as the time.
Other uses of date command
When you refer to the information for the date command using date –help in the terminal, you will find all the important sequences that you can combine to make use of the date command in more ways than mentioned in this article.
You can check out the full documentation at GNU.org for more info to explore the date command.
I hope that you can make good use of the date command when needed using the examples mentioned above. It is really a simple command with a lot of variations.
Let me know what you think about it in the comments below.