Wednesday, June 7, 2017

Date command in unix or Linux is one of important command to learn and master because we always need date information. no matter you want to know current date in unix or your bash script needs current date in unix for archiving purpose you need to use date command. In its simplest format date command shows the current date and time in unix while with sophisticated option we can extract many 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 date in unix and Linux. One important thing to note is that different implementation of unix date command may not be exactly 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:

This is simplest use of unix date command. just type 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 1414:31:35 MPST 2011

2) Example 2 - displaying Julian date in Unix

most of 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 year and 3 or last digit represent day of 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:

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 2114: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.

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
Thu

~/java date +%A
Thursday

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 unixdate.sh#!/bin/bash

DATE=`date`echo"Current date: $DATE"

~/java ./unixdate.sh
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.

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.