Friday, January 11, 2008

More of "date" command


A few more about "date" command, we can use its options for many occasions :-). Here are some of them.

1) Normal dates !
Today
$ date +%Y-%m-%d
2008-01-11

After 2 day
$ date +%Y-%m-%d -d "2 day"
2008-01-13

Before 2 day
$ date +%Y-%m-%d -d "-2 day"
2008-01-09

Yesterday
$ date +%Y-%m-%d -d "yesterday"
2008-01-10

Tomorrow
$ date +%Y-%m-%d -d "next day"
2008-01-12

After 3 weeks
$ date +%Y-%m-%d -d "3 weeks"
2008-02-01

Before 3 weeks
$ date +%Y-%m-%d -d "-3 weeks"
2007-12-21

One Year 3 days after
$ date -d "1 year 3 days"
Wed Jan 14 14:10:32 IST 2009

Last Sunday what was the date ?
$ date -d 'last sun'
Sun Jan 6 00:00:00 IST 2008

How about coming Sunday?
$ date -d 'sun'
Sun Jan 13 00:00:00 IST 2008

This year, what day does "Independence Day" fall on?
$ date --date='15 Aug' +%A
Friday

2) GMT Time?
$ date -u
Fri Jan 11 08:44:27 UTC 2008

3) Epoch time?

Number of seconds from 1970-01-01 UTC (epoch time,it is the number of seconds elapsed since midnight UTC of January 1, 1970, not counting leap seconds)
$ date +%s
1199968580

The opposite way conversion, from epoch time to date
$ date --date '1970-01-01 UTC 1199968580 seconds'
Thu Jan 10 18:06:20 IST 2008

Make a function in ~/.bash_profile

epochtime () {
date --date '1970-01-01 UTC '$1' seconds'
}

So that

$ epochtime 1199968580
Thu Jan 10 18:06:20 IST 2008

Perl/Python alternatives:

$ python
Python 2.3.5 (#2, Oct 16 2006, 19:19:48)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.ctime(1199968580)
'Thu Jan 10 18:06:20 2008'
>>>

$ perl -e "print scalar(localtime(1199968580))"
Thu Jan 10 18:06:20 2008

2 comments:

Srimay said...

Ha Ha Ha !!! It is as if we are writing 'literally' in prompt. '1 day', '2 days', 'next week'.....nice really :)

krushna said...

thanks for the post .We have ec2 (Amazon)servers which shows time in UTC zone.If I want to see indian time and date what parameter , I have to give with date command line.Thanks in advance.

Regards
Krushna

© Jadu Saikia www.UNIXCL.com