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!

Date

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hello,

Quick question...

date +%d"-"%m"-"%Y > today

gives me a file with todays date. How do I get yesterdays date?
 
Hmm,

This has gottrn me totally confused:

#!/usr/bin/sh
TZ="$TZ+24"
export TZ
day=`date +%m/%d/%y`
echo $day

It works beautifully enough, thank you, I just don't get it. Can somebody enlighten me please?
 
Hi BIS,

the TZ variable specifies the timezone your system is in (for example GMT). This is set in the installation procedure.

So to calculate any date in Korn shell you can use something like that:

#!/bin/ksh

[[ $# -ne 1 ]] && echo &quot;usage: x_days_back.ksh <how many days>&quot;


DAYS_OFF=$1 # how many days you want to go back?

if [[ -n $DAYS_OFF ]] # check for non-zero string
then
((OFFSET = 24 * $DAYS_OFF)) # calculate in 24h format
TZ=&quot;$TZ+$OFFSET&quot; # change timezone
backdate=`date +%d/%m/%y` # get the desired date
echo $backdate
fi

exit 0

You call the script like that:

./x_days_back.ksh 1 (for yesterday)
./x_days_back.ksh 3 (date three days back)

I hope I could help you with that explanation of TZ.

mrjazz [pc2]
 
Yes, thank you very much :)
 
Find &quot;pathname&quot; -name &quot;filename&quot; -mtime &quot;n&quot; /-atime &quot;n&quot;
n = modified/accessed n days

 
another question please....
I am using this in a script, several times...

#!/usr/bin/sh
TZ=&quot;$TZ+24&quot;
export TZ
day=`date +%m/%d/%y`
echo $day

but it seems that this command permanently sets the 'date' as yesterday.

Is there a way to reset the time after having done the above in a scripts, so I can use it again later with the same results as earlier (in the same scripts)? The reason is the script runs several sql statements that are date dependant.
 
BIS, just reset your TZ variable to whatever it was before you changed it:

export TZ=<old_value>

HTH.
 
is this correct then?

TZ=&quot;TZ+24&quot;
export TZ

some script

TZ=&quot;TZ-24&quot;
export TZ


because that does not seem to work.
 
I think probably the easiest way is to capture the original TZ in another variable and then reset it at the end, eg:

ORIG=$TZ ; export ORIG

<Whatever it is you're doing>

TZ=$ORIG ; export $TZ

HTH.
 
many many thanks, that worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top