Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Yesterday's or last month's date?

by renpai (Initiate)
on May 28, 2001 at 10:11 UTC ( #83700=perlquestion: print w/ replies, xml ) Need Help??
renpai has asked for the wisdom of the Perl Monks concerning the following question:

1. I want to get yesterday date. I have tried Use::Format qw(time2str,"%Y/%m/%d")but it doesn't work.
2. i want to generate a data in a program with date of last month.

Edit by tye to preserve formatting and since single-word titles complicate future simple searches

Comment on Yesterday's or last month's date?
Re: date
by mpolo (Chaplain) on May 28, 2001 at 10:23 UTC
    For (1), you could call time() to get the epoch seconds, subtract the number of seconds in a day (24*60*60), then use localtime() to get the day/month/year back.
Re: date
by BMaximus (Chaplain) on May 28, 2001 at 11:07 UTC
    Another way of doing it is to use Date::Calc.
    # getting yesterday's date use Date::Calc qw(Today_and_Now Today Add_Delta_YMD Add_Delta_DHMS); # use Date::Calc to subtract one day from today's date my ($year, $month, $day, $hours, $minutes, $seconds) = Add_Delta_DHMS( +Today_and_Now(),-1,0,0,0); # get date of last month by subtracting a month my ($year2, $month2, $day2) = Add_Delta_YMD(Today(),0,-1,0);
    You realy didn't make it clear as to whether or not you just wanted the month or you wanted the exact date of last month from the present.

    BMaximus

    Update: ick .. took out Add_Delta_Days .. don't bloat code with stuff your not using :)
Re: date
by tachyon (Chancellor) on May 28, 2001 at 11:15 UTC

    Here you are. You are fairly non specific about what consitutes "last month" so I have just used 30 days.

    cheers

    tachyon

    $time = time(); print "Yesterday (24 hours ago): ".localtime($time-24*60*60),"\n"; print "Last month (30 days ago): ".localtime($time-30*24*60*60),"\n";
Re: date
by diarmuid (Beadle) on May 28, 2001 at 17:07 UTC
    I would go with the Date::Format module eg:
    my @date = localtime(time); #get yesterdays date; $date[3]--; $date[6]--; my $run_date=strftime("%A %e %B %Y", @date);
    Similarly for last months date
    my @date = localtime(time); #get last months date; $date[4]--; my $run_date=strftime("%A %e %B %Y", @date);
    This will return something linke
    Sunday 27 May 2001
    
    Mess around with the template if you want a different format (The template is the "%A %e %B %Y" bit)

    Diarmuid

Re: date
by sierrathedog04 (Hermit) on May 28, 2001 at 17:43 UTC
    Normally the solution would be simply:
    my $yesterday = time() - (24 * 60 * 60); my ($day, $month, $year) = localtime($yesterday)[3,4,5];
    (Remember to add one to the value of $month and 1900 to the value of $year before printing them out.)

    However, yesterday's date may not be twenty-four hours ago. The day on which daylight savings time (DST) ends is 25 hours long; the day on which DST begins is only 23 hours long.

    Thus if you calculated the date 24 hours earlier during the last hour of the day DST ended you would get an erroneous result using the above formula, i.e., "yesterday's" and today's dates would be the same.

    Similarly if you calculated the date 24 hours earlier during the first hour of the first whole day after DST began you would also get an erroneous result using the above formula: "yesterday's" date would be two days before today's date.

    'perldoc perlfaq4' has a workaround by Russ Allbery that fixes the DST problem.

    Update: Corrected above to state that the formula at top would provide incorrect results in the last hour of the day DST ended, not the first hour of the day after it ended.

      Similar problem came up recently with a bbs running on a server in the U.S. for a Japanese audience. Daylight savings time and so on were involved. As long as you stick to unix time (seconds from epoch) you can calculate things like "24 hours ago" easily. Staying in Greenwich time until the end is useful.

      But if you want to do lots of other calculations (like I did for a recent timesheet tracking program that prints a calendar on top of a form) Date::Manip can do just about anything. Careful, it and CGI are some of the biggest modules in Perldom. (My process was 9 mb).

Re: date
by zeidrik (Scribe) on May 28, 2001 at 23:19 UTC
    Definitely Date::Calc is the best choice...
    You can find it on CPAN
    1. Yesterday: Add_Delta_Days
    ($year, $month, $day) = Add_Delta_Days(Today(), -1);
    2. What do You mean date of last month?
    Check Days_in_Month($year, $month) and do a cycle for that :)
Re: date
by ryan (Pilgrim) on May 29, 2001 at 06:37 UTC
    I have become quite happy with the Date::Manip library after reading the Perl Cookbook. I have found it slow for repetitive parsing, but it can make things very easy if you are constatly wanting odd dates using todays date as a basis.

    ParseDate can take an 'english like' string and then output that date in its own format of YYYYMMDDHH:MM:SS

    UnixDate is another function that converts ParseDate's output to a customisable format, similar to localtime as shown above, it lets you output things in a more customisable format if that is of any concern.

    use Date::Manip qw(ParseDate UnixDate); #examples giving epoch seconds $date = ParseDate("last month"); $date = ParseDate("yesterday"); $date = ParseDate("250 days ago"); $date = ParseDate("2nd tuesday in april"); #date formating example $date = UnixDate($date, "%d-%m-%Y"); ... ...

      Indeed, Date::Manip's ParseDate allows for a lot of flexibility. Here is an example of how I used ParseDate to create a very handy "sleep until" script.

Re: date
by Jamnet (Scribe) on May 29, 2001 at 16:21 UTC
    Hi renpai

    This is subroutine written by me which I use to get yesterdays date

    1.i # get yesterdays date my $date = &plsub_getdate(1); ## *** Get date number *** ## ## parameters - dday (optional) ## ## dday - difference day - $_[0] ## ## frmt - Date Format - $_[1] {WIP} ## sub getdatenum { use POSIX qw(strftime); if ($_[0]) { $date = strftime "%Y%m%d", gmtime(time - (86400 * $_[0])); } else { $date = strftime "%Y%m%d", gmtime; } return $date; }

    Now I suppose you will work to get 2.i. If still you have any problem mail me joel_divekar@yahoo.com

    Jamnet

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://83700]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-07-14 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (256 votes), past polls