UNIX Date Command Examples

by on January 27, 2009 · 19 comments· LAST UPDATED February 23, 2012

in

How do I view and set date under UNIX operating systems?

The date command under UNIX display date. It is also used to set date and time. You must be the super-user (root) to change the date and time.

UNIX Date Command Syntax

date
date "+format"

Task: Display Current Date and Time

Type the following command:

date

Sample outputs:

Tue Oct 27 15:35:08 CDT 2009

Task: Set The Current Time

To set the current time to 05:30:30, enter:

date 0530.30

Task: Set Date

Set the date to Oct 25, 12:45 a.m., enter:

date 10250045

Another example - set the current date and time to Oct 15, 2009 04:30 you type:

date --set="20091015 04:30"

Task: Generating Output

WARNING! These examples may not work on Linux computer running GNU/coreutiles date command. All examples are tested on HP-UX, AIX, Sun Solaris and other proprietary UNIX operating systems only.

Type the following command:

date '+DATE: %m/%d/%y%nTIME:%H:%M:%S'

Sample outputs:

DATE: 10/27/09
TIME:15:50:44

Try the following examples:

date "+%m/%d/%y"
date "+%Y%m%d"
date +'%-4.4h %2.1d %H:%M'

Unix Command Help

Type the following command to read the date command man page:

man date


If you would like to be kept up to date with our posts, you can follow us on Twitter, Facebook, Google+, or even by subscribing to our RSS Feed.


{ 19 comments… read them below or add one }

1 Philippe Petrinko March 15, 2010 at 4:32 pm

@Vivek – Do you confirm this one ? My date ((GNU coreutils) 6.10) does not like it much.

date +'%-4.4h %2.1d %H:%M'

What does it do on your system?

Reply

2 Vivek Gite March 15, 2010 at 6:15 pm

@Philippe,

I think I used that one on proprietary AIX or may be on HP-UX UNIX box. The month field is four characters long, left side. Same goes for the day (2 chars long). It will not work on *GNU coreutils*. You can try them on HP-UX or AIX and should provide output as follows:

mmmm    dd HH:MM

Reply

3 Philippe Petrinko March 15, 2010 at 6:19 pm

@Vivek: Then, would you consider adding some warning/advice/comment to prevent your readers loosing time (and hairs ;-) ).

Reply

4 Vivek Gite March 15, 2010 at 6:44 pm

Title does says “UNIX Date Command Examples” and Linux != UNIX. Nevertheless, your suggestion is accepted :). Thanks!

Reply

5 priyanka July 5, 2011 at 10:25 am

very nice contain in this site……….useful very much in study…………..

Reply

6 learner July 13, 2011 at 11:04 pm

will date –version work in linux? why is the output different that echo “–version” | date

Reply

7 Philippe Petrinko July 14, 2011 at 2:20 pm

Hi Learner,

Right way to give parameters on command line is:

date –version

When you want a program to use some input from a pipe is:

echo “some text used as input” | tr “[[:lower:]]” “[[:upper:]]”

But these are two really different functionalities. They are not equivalent at all, so they cannot yield the same.

– Philippe

Reply

8 Learner July 14, 2011 at 2:24 pm

Consider – echo “–version” | date
here echo “–version” will give the output –version , which will act as an input to date. .
so we should get date –version.

But we do not get the same output from echo “–version” | date
and date –version. Can you please explain why the output is different.

Reply

9 Philippe Petrinko July 14, 2011 at 3:53 pm

You confuse [standard input usage] with [parameters on the command line] which are different kind of inputs, which cannot be exchanged.

The genuine way to give parameters to a command is to give them on the program call, which is made on the command line.

There a other ways to achieve parameter passing to a command, such as using a specific file, or using environment variables.

On the other hand, standard input is commonly used to give _data_ to process, not parameters.

As a matter of facts, [date] command does make any use of standard input.
So you can pipe _anything_ you want, [date] won’t ever use it. You might have pipe (with echo) any text, [date] won’t use it.

Make sure by Reading The Fantastic Manual,
by issuing the command: man date

Do you think you get the point?

Reply

10 amsurre February 22, 2012 at 1:14 pm

How to overcome missing -d option (present on gnu date – linux) on unix based OS ..

date -d ’1 hour ago”

Reply

11 Philippe Petrinko February 22, 2012 at 9:49 pm

Hi vivek,

Typo here: “other propitiatory UNIX operating systems only”
“propitiatory” should be: “proprietary”

;-)
KUTGW

Reply

12 Vivek Gite February 23, 2012 at 6:25 am

Thanks for the heads up!!!

Reply

13 bharath January 16, 2013 at 10:50 am

I am using these 2 commands in my unix shell sxript. when i do subtract , and if current month is jan, the jan -1 month should give me dec 2012, but getting it wrong. can you correct me?

mydate=`date +%Y-%m-%d`
mydate1=`date +%Y`’-0′$((`date +%m`-1))’-’`date +%d`

OrderedDate BETWEEN
DATE(‘$mydate1 HKT’) AND
DATE(‘$mydate HKT’)

Reply

14 Vivek Gite February 23, 2012 at 6:25 am

GNU has more advantages over the traditional Unix date command. Try:

yest

Try TZ to get y’day and tomorrow:

TZ=IST24 date   # y'day
TZ=IST-24 date # tomorrow

Where,

  • IST is my timezone and 24 is offset.

Write a perl / shell / python script to calculate date :)

Reply

15 CBee January 16, 2013 at 10:51 am

Since about 15 years, if Ineed to maintain multiple unix variants (sun/solaris, hp-ux etc), I most frequently find myself installing the gnu tools and using them. They are available for all unix system that come with a c-compiler…

Reply

16 C. Beerse April 4, 2012 at 9:32 pm

Nice page on unix date command. Actually I was looking for the formats to be used by the –set=”" and/or –date=”" options as available on cygwin and/or linux.

Reason: I intend to use `date -date=”input date spec” “+%Y%m%d%H%M%S”` to translate date specifications to the format I like/need.

I already found `date –date=@12345678` which expects the epoc time (seconds since 1 jan 1970). Also nice are ‘yesterday’, ‘now’, ‘tomorrow’, ‘last friday’ and such.

Reply

17 myName August 30, 2012 at 11:15 am

Thanks

Reply

18 bharath January 16, 2013 at 9:49 am

I am using these 2 commands in my unix shell sxript. when i do subtract , and if current month is jan, the jan -1 month should give me dec 2012, but getting it wrong. can you correct me?

mydate=`date +%Y-%m-%d`
mydate1=`date +%Y`’-0′$((`date +%m`-1))’-'`date +%d`

OrderedDate BETWEEN
DATE(‘$mydate1 HKT’) AND
DATE(‘$mydate HKT’)

Reply

19 CBee January 16, 2013 at 11:05 am

You are twiggling with the output using text in the output string. Keep in mind, the main purpose of the command `date` is to fetch the epoc (seconds since 00:00gmt, 1 jan 1970) and to translate that to the human readable version in the current timezone.

With that, most date commands can use different input to be used in stead of the current epoc. gun date (gnu tools, linux, cygwin etc.) does have a -d option to use an alternate date. There you can use `date -d lastmonth` to get the date from a month ago. On others you might only be able to use an alternate epoc. Then you can use (up to a limit) the current epoc minus the number of seconds in a month. To make your script easy, best use 4 or 5 weeks for this (5 * 24 * 60 * 60)

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: