The date command is part of the Coreutils package, and it is mainly used to get the date in a different type of format with various available options.
At first, the date command may seem like a simple utility to you, but once you try to execute the date command with different utilities, you will realize the real power.
A date command can be handy in bash scripting, backup, and the limation is your imagination.
In this article, you will see the basic to advanced usage of the date command in Linux.
Table of Contents
Date command Basic syntax
The basic syntax of the date command is simple as follows.
$ date [OPTIONS]...[+FORMAT]
- [OPTIONS] :- you can use various options such as –date.
- [+FORMAT] :- you can use 30+ formats to get different types of date formats.
Basic use of Date command
If you type the “date
” command in a terminal, it will print the following details such as weekday, date with month, year, time separated with a colon, and the last timezone.
$ date
The above command can also be used to view your system’s date and time and if you notice the above output is different from UTC (Coordinated Universal Time) or Greenwich Mean Time (GMT) timezone.
View UTC or Greenwich Mean Time using date
As you know, UTC is the only way to maintain the date & time after the system gets shut down. To view the UTC timezone data, pass the below command with option -u
.
$ date -u
The output format is similar to the default date format.
Get shorter version of date and time
To get a shorter version of default date output, use -R options that accept rfc-2822 standard, and it is commonly used for ARPA Internet Text Messages or email header.
$ date -R
Can you see the difference, right?
Go to specific Date using –date or -d
Are you curious to know on which day of the week you were born? Yes, that is also possible to check using the –date option. To get the details pass the following command in a manner of [MM/D/YYYY HH:MM] or [FEB DATE YEAR]
$ date --date="04/19/1994 18:45"
Alternatively, you can use -d and syntax are same as –date options.
$ date -d "04/19/1994 18:45"
Check upcoming or last week, month, day and year
Again -d option can be helpful to fetch information of certain periods, which include past week, no of weeks, month, and many more.
A partyer always waits for the weekend. If you are eagerly waiting for Friday night, then pass the below command.
$ date -d "friday"
I want to check on which day Christmas eve is coming.
$ date -d "25 dec"
These were the few examples of the -d [strings] option. You can also use other strings like “n weeks”, “last week”, “n months”, “tomorrow”, etc.
Convert Different timezone to local time
Suppose you want to attend a zoom meeting which will be happening on upcoming Monday at 10:45 EST/ New York. You can easily convert this using the date command, and for that, you should know the respective timezone.
If you are not sure about the timezone, take the help of timedatectl command to view a list of timezones.
$ timedatectl list-timezones
Type the following command on your terminal screen to convert the timezone.
$ date -d 'TZ="America/New_York" 10:45'
Aha! I forgot to put “Monday”, let me fix this.
Find when file was modified
There are multiple ways to check when the file has been modified or tampered, and the date command is one of them. To get the modification time type the below code.
$ date -r /etc/host.conf
List of available date format
There are multiple formats is available to modify the default format and we have listed some of the formats. Please go through the list.
Date Format Option | Meaning | Example Output |
---|---|---|
date +%a | locale’s abbr. weekday name | Sun |
date +%A | locale’s full weekday name | Sunday |
date +%b | locale’s abbr. month name | Jan |
date +%B | locale’s full month name | January |
date +%d | day of month | 01 |
date +%F | YYYY-MM-DD ISO format | 2022-01-10 |
date +%D | MM/DD/YY date format | 01/10/22 |
date +%m-%d-%Y | Print date in MM-DD-YYYY | 01-10-2022 |
date +%F | YYYY-MM-DD date format | 2020-05-09 |
date +%H | HH: format time in 24 hrs | 13 |
date +%I | HH: format time in 12 hrs | 12 |
date +%j | Day of the year in 366 | 09 |
date +%m | Month in number | 01 |
date +%M | Minutes | 59 |
date +%N | Time in a nanosecond(Create Timestamp) | 760818600 |
date +%r | Time in AM/PM | 08:26:55 PM IST |
date +%S | Seconds | 60 |
date +%T | Time format in 24hrs with seconds | 20:25:45 |
date +%u | Day of the week from 52 weeks | 2 |
date +%x | Get Date in local format | 09/01/2022 |
date +%Y | Year | 2022 |
date +%:z | Time zone ISO code number | +05:30 |
date +%Z | Time zone ISO code | IST |
How to modify the default format
Whenever you want to modify the default format use + character along with a respective format. To understand you better, we will illustrate.
$ date "+%A %d/%m/%C%y %H:%M:%S" -d "1994-04-19 18:45:00"
I’m sure you will get the format code.
We will format the output with some of the extra text, with a current date. here also don’t forget to use + otherwise, you will get the error “date: invalid date ‘Today’s date is: %x’.
$ date +"Today's date is: %x"
Get timestamp and convert to standard time
The date command can be used to create a Unix timestamp, or you can say so-called epoch converter. If you never heard about the epoch, I’ll explain to you the epoch or UNIX timestamp, it is the number of seconds lapse from January 1, 1970, at 00:00:00 UTC.
To create a timestamp of a current time, type the following code.
$ date +%s
That’s not the case every time, suppose I want to create a timestamp for 10th Feb 1994, how do I use the %s format to create a timestamp?
$ date -d "10 feb 1994" +"%s"
Fair enough, but how do I read Unix timestamp or epoch time in Linux? To read timestamp use -d options and attach timestamp with prefix @.
$ date -d @760818600
Generate random number using date
Initially, I told you that the date command can do a lot more than just simple date printing. Let’s say you want to generate random timestamps or random dates, how would you generate any guesses? No, as far as I know, you can also use the date command to generate a random timestamp in milliseconds with the help of sed command.
Pass the below code to generate a random number and run the below code multiple times to see different timestamps or numbers.
$ date +%N | sed -e 's/000$//' -e 's/^0//'
Create random timestamp Alias
Every time writing a long command doesn’t make sense, it’s better to create an alias and use it whenever you want.
To create an alias type name that you want to invoke, we named our alias randomTimeStamp and copied the above code, or else you can use the below code.
$ alias randomTimeStamp='date +%N | sed -e 's/000$//' -e 's/^0//''
Once you are done type the alias name to create a random timestamp.
This alias will not be stored for a longer period once you close the terminal alias will get purged. To prevent it copy the alias data to ~/.bashrc.
Leverage date using redirection
These options may help you to generate a date and time on the file. To work out you need to use redirection symbols with preformat date code.
$ date +'%x %T' >> sampleData.txt
Whenever you need to add a date and time run the above command or else you can set an alias.
Check Leap year with date command
We can even check leap year using the date command. For that first create a bash file, use any command-line editor and paste the below command to check a leap year between 2020 to 2040.
#!bin/bash counter=0 for y in {2020..2040}; do date -d $y-02-29 &>/dev/null && let counter++ && echo Hey Listen, $y is leap year.; done echo Total number of leap year is $counter
Wrap up
We have tried to cover all the aspects of the date command like view current date and time with different types of formats, creating epoch converter or UNIX timestamps, and by using of bash script we are able to check leap year using the date command.
This command can be handy if you are working with CLI.
If are interested to learn more, please check man date.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.