Wednesday, June 7, 2017

10 Examples of Date Command in Linux and UNIX

Date command in UNIX or Linux is one of the important commands to learn and master because we always need date information. no matter you want to know the current date in UNIX or your bash script needs a current date in UNIX for archiving purposes you need to use date command. In its simplest format date command shows the current date and time in UNIX while with a sophisticated option we can extract much useful information from UNIX date command. In this Unix command tutorial, we will see some useful tips on using date command in UNIX and learn more around the date in UNIX and Linux.
One important thing to note is that different implementation of UNIX date command may not be exactly the same e.g. AIX version of date command doesn't support option "-d" or "date". Anyway let's see some example of UNIX date command:

By the way, if you are new to Linux then I also suggest you go through a comprehensive Linux course to learn some basics commands and fundamentals like Linux file system, permissions, and other basic things. 

If you need an online course, I highly recommend Linux Mastery: Master the Linux Command Line in 11.5 Hours on Udemy. It's a very practical and hands-on course to learn Linux fundamentals in a quick time. It's also very affordable and you can buy in just $10 on Udemy flash sales which happen every now and then. 

10 Examples of Date Command in Linux and UNIX

By the way, this is the fifth article in my series of covering examples of important and frequently used commands e.g. find examples, grep examples, chmod example, and sort command example.

1) Example 1 -  displaying current date and time in Unix

This is the simplest use of the UNIX date command. just type the date in the command prompt and it will show you the current date and time in the timezone your machine is.

~/java date
Thu Jul 14 14:31:35 MPST 2011

2) Example 2 - displaying Julian date in Unix

Date command examples in Unix and Linuxmost of the java stock trading application running under Linux use Julian date format to store date information. Julian dates are made of 5 digits 2 of which represent the year and 3 or last digit represent day of the year. for example, 14th July 20111 will be represented by 11195 in Julian format because 14th July is the 195th day of the year. we can get Julian date by using date command in unix as shown in the below example of unix date command:

Converting current date into Julian format in unix
~/java date +%j

Converting a specific date into Julian format in unix
~/java date -d "2011/07/13" +%j

this way you can get yesterdays Julian date or tomorrow's Julian date in Unix though I have heard that "-d" option of date is not supported in AIX so this may not work in AIX version of Unix.

3) Example 3 - displaying current date in various format in unix

Unix date command is very powerful and can show date in various format. in this section of unix date tutorial we will convert date into some commonly used date format in unix:

Showing date in YYYYMMDD format in Unix
~/java date +%Y%m%d

here %Y shows year in four digit ( you can use %y (small letter) for displaying just 2 digit of year)  %m  shows two digit of month as 01..12 and %d represent day of month.

Showing date in DD/MM/YYYY format in unix
~/java date +%d\/%m\/%Y

here again %d is for day %m is for month and %Y is for four digit year if you want to show 2 digit use small case y e.g. %y

Showing date in DD-MM-YY format in unix
~/java date +%d\-%m\-%Y

quite similar to above approach just chaged the "/" to "-", you can change it to any character as per your wish and until unix accept it.

displaying date in MM/DD/YYYY format in Unix
what do you guys think would it be difficult no right ? just exchange place of "%m" and "%d" and you are ready as shown in below example of date command in unix

~/java date +%m\/%d\/%Y

displaying date in YYYY-MM-DD format is exactly similar to above approach just change the order of year and month.

4) How to find date 7 days before current date in Unix

This is really nice of gnu version of unix date command you can easily get what's the date 4 days before or 5 days before by using date command in unix. here is example:

date after 7 days in unix
~/java date -d "-7 days"
Thu Jul  7 14:54:36 MPST 2011

by using this way you can find any date which is any days apart from a given current date in unix

5) How to get date 7 days after current day in Unix and Linux

As shown above you can get the date after 7 days in unix by using gnu version of date command in unix, here is an example of getting date 7 days after current date:

~/java date -d "7 days"
Thu Jul 21 14:54:15 MPST 2011

this way you can find any date which is 7 days past of a given current date in unix

6) Setting date time into unix

setting date is easy though I don't recommend to do it if you have unix admins. Also changing date requires root access which you might not have with you anyway we can use unix date command to change the date, just provide new date in the specified format and your current date will be set to new date.

format of setting date time into unix

date mmddhhmi

where mm = month
      dd = day of month
      hh = hour
      mi = minute
Example of changing date time in unix

~/java date 07240222
Sun Jul 24 02:22:00 MPST 2011

above example of date command will set the current date to 24th July and time to 02:22, we don't provide year information and date uses current year by default.

7) How to find timezone by using unix date command - Example

unix date command can display timezone related information also. it provides following option to display timezone
  %:z  +hh:mm numeric time zone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

Now let's see some example of unix date command to display timezone

~/java date +%Z  (this will display timezone in common format e.g. EST, GMT)

~/java date +%z  (this option of unix date command will display timezone respective of GMT)

8) How to show day of week in Unix using date command - Exmaple

you can show current date, day of week name using unix date command  by using options "%a" and "%A". small case is used to display day in short format while capital one is used to display full name of day. see the below example of date command in unix for display day of week :

~/java date +%a

~/java date +%A

9) Date command in Unix for showing current time in Various format

time is another important information which we can get from unix date command. by default date command in unix display both date and time information but we can use various options provided by date command to display only time information. let's see some example of showing time using unix date command:

10) Date to command for showing time in format HH:MM:SS

As shown above on showing date in different format in unix you can also display time on different format by applying same approach. you just need to remember following time related options of unix date command

%H   hour (00..23)
%M   minute (00..59)
%S   second (00..60)

that's all for now guys I hope you will be more comfortable with date command in unix after going through and trying these examples. you can get more information about date command in unix by using "man date" or "date --help" which will also list all the options supported by unix date command.

Tip: just remember that unix date command is mostly used inside bash script to get the date information , so its worth mentioning here how to execute date command from bash script, you just need to specify date command in back quotes . see the example bash script  :

~/java cat

echo "Current date: $DATE"

~/java ./
Current date: Sun Jul 24 02:30:50 MPST 2011

So that was all about different examples of date command. We have seen how we can format date in Unix, how we can convert date into different timezone in Unix, how we can get current date and time inside bash script and several other examples of date command. Let me know if you have any other usage of date command which is not covered here.

Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career

Other Linux tutorials from Javarevisited:


Vidha said...

Hi, how do you find yesterday's date in Unix? , I have to write a bash shell script where I need both yesterday and tomorrow date to print, please give me exact date command.

varun said...

Hi Vibha,

You can use
date +%d/%m/%Y -d "-1 days" for yesterday's date and date +%d/%m/%Y -d "1 days" for tomorrow's date.

Anonymous said...

is there any separate unix or linux command for time ? I know by using date we can get time with timezone but just in case if there is any better utility exists for getting time?

Anonymous said...

One of the example you missed is about changing timezone which is used by DATE command, Date command uses environment variable "TZ" to find out timezone and print date on that time format. just do export TZ='PST' before printing date in PST timezone.

Post a Comment

Pepperjam Verification