Operating System - HP-UX
1833178 Members
2693 Online
110051 Solutions
New Discussion

Shell Script (Get old date from current date)

 
SOLVED
Go to solution
Chen Yingjie
Frequent Advisor

Shell Script (Get old date from current date)

I want to create a shell script to get
old date from current date.

For example,

I can get the current by `/usr/bin/date +%Y%m%d`.

How to get the old date(14 days before) from above current date?

Thanks for advise.
11 REPLIES 11
Fred Ruffet
Honored Contributor

Re: Shell Script (Get old date from current date)

You can use caljd from A.Clay Stephenson.
In shell flavor :
http://mirrors.develooper.com/hpux/caljd-2.23.sh
or perl one :
http://mirrors.develooper.com/hpux/caljd-2.2.pl

It's a you-can-do-anything-with-date program.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Chen Yingjie
Frequent Advisor

Re: Shell Script (Get old date from current date)

Thanks for reply.

Is there another way to slove this question?

Chen
Fred Ruffet
Honored Contributor

Re: Shell Script (Get old date from current date)

Yes. One more : Install GNU version of date. You will be then able to use something like :
date '14 days ago'

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Script (Get old date from current date)

Your problem is trivially easy if you use the afore-mentioned caljd.sh utility.

#!/usr/bin/sh

DATEMINUS14=$(caljd.sh -y -s $(caljd.sh -p 14))
echo "14 days ago was ${DATEMINUS14}"

All of the calculations are done in just two functions cal_jdate and jdate_cal so if you like you can simply yank those out and put themn in your script. Invode as caljd.sh -u for full usage and many examples.


If it ain't broke, I can fix that.
Manuel Contreras
Regular Advisor

Re: Shell Script (Get old date from current date)

I really like this solution, got It from this site.
enjoy,
manuel


today
$ date '+%b-%d'
Aug-13

- 1 day
$ perl -e 'print scalar localtime (time -1 * 86400),"\n"'
Thu Aug 12 14:03:33 2004

- 13 days
$ perl -e 'print scalar localtime (time -13 * 86400),"\n"'
Sat Jul 31 14:04:01 2004

yesterday
$ perl -e 'print scalar localtime (time -1 * 86400),"\n"' | awk '{print $2"-"$3}'
Aug-12

tomorrow
$ perl -e 'print scalar localtime (time +1 * 86400),"\n"' | awk '{print $2"-"$3}'
Aug-14
Chen Yingjie
Frequent Advisor

Re: Shell Script (Get old date from current date)

I tried the Perl command listed above.
But it doesnot work on my server.

My server's OS is HP-UX 11.00.

Chen
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Shell Script (Get old date from current date)

Well, if you insist on a Perl solution, here you go in your format but caljd.sh (or caljd.pl) is much easier:

#!/usr/bin/sh

DT14BEFORE=$(perl -e '($year,$mon,$day) = (localtime(time() - (14 * 86400)))[5,4,3]; printf("%04d%02d%02d\n",$year + 1900,$mon + 1,$day)') # all of this is one line
echo "14 Days ago = ${DT14BEFORE}"

If it ain't broke, I can fix that.
Chen Yingjie
Frequent Advisor

Re: Shell Script (Get old date from current date)

Thank you sir.

I have confirmed it work.

Chen Yingjie
Markus Miller
New Member

Re: Shell Script (Get old date from current date)

Hi,

there is a little script that calculates the date in the past.

# Comand line argument: Amount of days in the past
PAST=$1

DATE=`date +%Y%m%d`
DATE=`expr $DATE - $PAST`
YEAR=`echo $DATE | cut -b 1,2,3,4`
DAY=`echo $DATE | cut -b 7,8`
MONTH=`echo $DATE | cut -b 5,6`
if [ "$DAY" = "00" ]
then
DATE=`expr $DATE - 100`
MONTH=`echo $DATE | cut -b 5,6`
if [ "$MONTH" = "00" ]
then
MONTH=12
DATE=`expr $DATE - 10000`
YEAR=`echo $DATE | cut -b 1,2,3,4`
fi
x=`cal $MONTH $YEAR`
DAY=`(for i in $x; do echo $i; done;) | tail -1`
fi

YEAR=`echo $YEAR | cut -b 3,4`
HOUR=`date +%H`
MINUTE=`date +%M`
# Here you can change the output
DATE="${MONTH}${DAY}${HOUR}${MINUTE}${YEAR}"

echo $DATE

Regards
Markus
Manuel Contreras
Regular Advisor

Re: Shell Script (Get old date from current date)

Chen, I just ran it on our 10.20 box:

manny> perl -e 'print scalar localtime (time -1 * 86400),"\n"'
Wed Dec 1 07:59:25 2004
manny> date
Thu Dec 2 07:59:28 PST 2004



maybe it's the version of perl:
manny> perl -v

This is perl, v5.6.0 built for 9000/777-hpux



the following is from an 11.00 box:
> perl -e 'print scalar localtime (time -1* 86400),"\n"'
Wed Dec 1 08:01:42 2004
> date
Thu Dec 2 08:01:44 PST 2004
Chen Yingjie
Frequent Advisor

Re: Shell Script (Get old date from current date)

Thanks for all reply.