Date command usage in Linux

  • Post last modified:January 11, 2022
  • Reading time:8 mins read
  • Post author:
  • Post comments:0 Comments

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 scriptingbackup, and the limation is your imagination.

In this article, you will see the basic to advanced usage of the date command in Linux.

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
Default Date
Default Date & Time

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
date command to get utc timezone-compressed
Date command to get UTC timezone.

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
shorter version of date and time
A shorter version of the date and time

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"
Go to specific Date using --date option
Go to a specific date using the –date option.

Alternatively, you can use -d and syntax are same as –date options.

$ date -d "04/19/1994 18:45"
Go to specific Date using -d
Go to a specific Date using -d

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"
check day of week
check day of week

I want to check on which day Christmas eve is coming.

$ date -d "25 dec"
check month day using date
check day of the month

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'
convert timezone using date
convert timezone using date

Aha! I forgot to put “Monday”, let me fix this.

convert timezone
convert timezone

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
modification time check using date command
modification time check using the date command

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 OptionMeaningExample Output
date +%alocale’s abbr. weekday nameSun
date +%Alocale’s full weekday nameSunday
date +%blocale’s abbr. month nameJan
date +%Blocale’s full month nameJanuary
date +%dday of month01
date +%FYYYY-MM-DD ISO format2022-01-10
date +%DMM/DD/YY date format01/10/22
date +%m-%d-%YPrint date in MM-DD-YYYY01-10-2022
date +%FYYYY-MM-DD date format2020-05-09
date +%HHH: format time in 24 hrs13
date +%IHH: format time in 12 hrs12
date +%jDay of the year in 36609
date +%mMonth in number01
date +%MMinutes59
date +%N Time in a nanosecond(Create Timestamp)760818600
date +%rTime in AM/PM08:26:55 PM IST
date +%SSeconds60
date +%TTime format in 24hrs with seconds20:25:45
date +%uDay of the week from 52 weeks2
date +%xGet Date in local format09/01/2022
date +%YYear2022
date +%:zTime zone ISO code number+05:30
date +%ZTime zone ISO codeIST

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"
modify date format
Modify date format

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"
format date output using %x
format date output using %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
create timestamp using date
create timestamp using date

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"
custom timestamp create using date
custom timestamp create using the date command

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
read timestap in linux using date command
read timestamp in Linux using the date command

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
create random timestamp

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.

create alias in date command
Create random timestamp Alias

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.

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
leap year check
Check Leap year with the date command

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.

Leave a Reply