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

Time module

Status
Not open for further replies.

mikedaruke

Technical User
Mar 14, 2005
199
US
Quick question. I know using the time module I can get the date (month, day, year..etc)

I need to get yesterdays date. I tried the -1 to the current date, but of course on the 1st of the month, it would think yesterday is 0. I need to have it look at todays date: 4/1/05 and tell me yesterday was 3/31/05. I am sure I can do some caculations to figure this out. Just wondering if it was an easy way, or another module or fuction I can use. If not any ideas? Thanks!
 
I'd recommend using the POSIX module using strftime(). One can manipulate just about anything with it.
 
The time function returns the current time in seconds since the epoch.
There are 86400 (60*60*24) seconds in a day.
So if you subtract the number of seconds in a day from the current time, you get the time in seconds one day ago.
Then feed that to the localtime function.
Code:
#!perl
use strict;
use warnings;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);
$mon = $mon + 1; 
$year = $year + 1900;
printf "%02d/%02d/%04d\n", $mon, $mday, $year;
There's also the Date::Calc module.

HTH
 
While there is a Date::Calc module, there is also a Date::Manip module. I am not saying which one to use, but you should review both to see which suits you best.


Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top