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 variable manipulation 4

Status
Not open for further replies.

scooter6

IS-IT--Management
Jul 10, 2001
44
US
I am finishing up a script that sets the following variable:

DAY=`date +%m%d`

which of course outputs as &quot;0712&quot; <-- for today,July 12

I need to set that variable as YESTERDAY's date,
for a script that will run shortly after midnight.

How can I do this WITHOUT it dropping the lead &quot;0&quot;

please help....thanks
 
At end of your program, echo today's date into a file called
.yesterday.
 
well, i'm no ace, but this might work for you (i ckd it out)

# day=`date +%m%d`
# echo $day
0713
# yday=`expr $day - 1`
# echo $yday
712
# yday0=&quot;0$yday&quot;
# echo $yday0
0712
#

or you might hafta to add:
# y=`expr $yday0`
# echo $y
0712
#
good luck :)
 
Don't forget leap years, february, end of century ...
you can also try this,

env TZ=&quot;$TZ+24&quot; date +%d

for same cases is more simple.

Regards,

Carlos Almeida,
 
Using the KISS principal and the ever-so-cool 'printf' command, try this ...

# YESTERDAY=`printf &quot;%04&quot; $DAY`


gafling
 
New FAQ added - comments and additions please :) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
looking for the same type of thing but need to set the date in a format. Can anyone help below...

# day=`date +%m/%d/%y`
# echo $day
07/13/01

# yday=`expr $day - 1`
# echo $yday
712

--> what I want is the date to look like 07/12/01


 
env TZ=&quot;$TZ+24&quot; date +%m/%d/%y
or assunming small shell script &quot;day.sh&quot;:

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

# ./day.sh
/08/12/01

Regards,

Carlos Almeida.
 
hi cfsaleida,

can u show exactly what must to in the script file to get yesterday's date?
must take into account if the date is 1st of the month.

regards
orian
 
just put this into a file a give execute permissions.

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

That’s all you need, variable day in the &quot;script&quot; has the yesterday date, in this way we don't need to consider if is the first day of the month, if is February, end of century, etc, because we are using the standard date function, the &quot;trick&quot; is TZ environment variable...

Regards,

Carlos Almeida,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top