Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculations with date

Status
Not open for further replies.

Kalin

Programmer
Jul 24, 2001
76
NL
Hi,

I'm looking for a way to calculate the date and year of last month which i need in a variable.

I'm using digital Unix and a Korn Shell.

Greetz, Grtz,

Kalin
 
P.S. for instance running the script today would return 0901 where 09 is september and 01 is 2001. Grtz,

Kalin
 
Hi,
you might want to look at...

thread80-105565 or faq80-953

which was spawned from that thread.


It basically explains how to use the Date command to calculate yesterday. You don't want yesterday but it shows the proper syntax for the date command.

your example would be

date "+%m%y" ( 0901 or this might return 092001 )

do a man date to get a complete list of the options. Also Case matters....

%m == month
%M == minutes

so make sure you use the correct options.

 
One way might be to write the date on the 28th of each month to a file as the output of a date +%m%d command and then reference that file in your script the next month. Of course, this assumes that you won't need to run the script on the 29th, 30th and 31st of any relevant months :)! HTH.
 
Hi,
I forgot you wanted last month.

mon=`date +%m`
year=`date +%y`
lastmon=`expr $mon - 1`
lastyear=$year
if [ $lastmon == 0 ] ; then
$lastmon = 12
$lastyear=`expr $year - 1`
if [ $lastyear == -1 ] ; then
$lastyear == 99
fi
fi

echo ${lastmon}${lastyear}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top