1832534 Members
8005 Online
110043 Solutions
New Discussion

Re: Date manipulating

 
Vasudevan MV
Frequent Advisor

Date manipulating

Hi,

I have tried with "echo `TZ=IST+24 date`" to get previous day's date, but this doesn't work if the current time is (HH:MM) 00:00 to 05:00 AM, IST.
Can any one suggest me the better & simple command to get the previous day's date?.

Vasu
8 REPLIES 8
Paula J Frazer-Campbell
Honored Contributor

Re: Date manipulating

Hi

This is a nice script written by Tom Danzig which he posted some time ago.

HTH

Paula
If you can spell SysAdmin then you is one - anon
Robin Wakefield
Honored Contributor

Re: Date manipulating

Hi Vasu,

If you have perl:

perl -e 'print scalar (localtime(time-86400)),"\n";'

Rgds, Robin
Vasudevan MV
Frequent Advisor

Re: Date manipulating

Hi Paula,

Thanks for your solution. But I am looking for single line command to get the previous day's date.

Vasu
Santosh Nair_1
Honored Contributor

Re: Date manipulating

Vasu,

I don't believe there is a one line solution for what you're asking. Espeically not in shell script. However, its quite easy in perl as Robin already pointed out...but not sure if Perl is an option for you.

-Santosh
Life is what's happening while you're busy making other plans
Robin Wakefield
Honored Contributor

Re: Date manipulating

Hi Vasu,

If the perl solution is no good, we should find out why your original method is failing.

What's it show between 0:00 and 5:00? Can you output the IST timezone details in your /usr/lib/tztab. This *may* help...

Thanks, Robin.
Santosh Nair_1
Honored Contributor

Re: Date manipulating

Actually, I was just thinking about this. Your timezone should be IST-5:30. So in the proper string you should be using is
echo $(TZ=IST+18:30 date)

-Santosh
Life is what's happening while you're busy making other plans
Vasudevan MV
Frequent Advisor

Re: Date manipulating

Santosh,

I have tried that too "echo $(TZ=IST+18:30 date)"... but it should be generic.

Thanks
Vasu
A. Clay Stephenson
Acclaimed Contributor

Re: Date manipulating

Hi Vasu:

I've had a script I've used for many years that converts calendar dates to Julian Days and back. Because I've seen so many requests for dates prior to today or 37 days from today, I've modified it with new args -p days for previous days or -n for next days. When suppiled with mm dd yyyy it converts to a Julian Day; when supplied with a Julian day it
outputs mm dd yyyy. No args and it converts today to a Julian Day.

This should work for you:
caljd.sh `caljd.sh -p 1`

The second calhd.sh takes today's date - 1 and converts it to a Julian Day which is in turn fed to the outer caljd.sh to output a mm dd yyyy.

To capture the output in a variable and get around the nested backquotes, try this:

PREV_DAY=$(caljd.sh `caljd.sh -p 1`)

If you are building file names and want to remove the spaces add the -s arg.

Regards, Clay


If it ain't broke, I can fix that.