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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I calculate yesterdays date?

Tips and Tricks

How do I calculate yesterdays date?

by  MikeLacey  Posted    (Edited  )
Using Perl:
[tt]
#!/local/bin/perl -w

use strict;
my $today = localtime();
my $yesterday = localtime(time-86400);

print "today = $today\n";
print "yesterday = $yesterday\n";
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($yesterday);
$mon++; $year+=1900;
$day = '0' .= $day if length($day)==1;
$mon = '0' .= $mon if length($mon)==1;
print "yesterday was also $day/$mon/$year\n";
[/tt]

Using the Korn shell (ksh) or Posix shell (sh)
[tt]
#!/usr/bin/sh
TZ="$TZ+24"
export TZ
day=`date +%m/%d/%y`
echo $day
[/tt]

Thanks to Carlos Almeida for the shell solution
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top