Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Find 30 days from today's date

by golux (Pilgrim)
on Jun 07, 2013 at 15:58 UTC ( #1037693=note: print w/ replies, xml ) Need Help??


in reply to Find 30 days from today's date

You're already using Date::Calc, so I'd go with Add_Delta_Days:

#!/usr/bin/perl -w use strict; use warnings; use Date::Calc qw{ :all }; my ($year, $mon, $day) = Today(); show_date("Today's date", $year, $mon, $day); ($year, $mon, $day) = Add_Delta_Days($year, $mon, $day, -30); show_date("30 days ago", $year, $mon, $day); sub show_date { my ($label, $year, $mon, $day) = @_; my $date = sprintf "%04d-%02d-%02d", $year, $mon, $day; print "$label: $date\n"; } __END__ [Output] Today's date: 2013-06-07 30 days ago: 2013-05-08
Update:    Oh, now I see why you're getting different answers. It's because in:
$date[3] -= 4 * 7; ... my $month_ago_3 = strftime('%Y-%m-%d', localtime(time - 4 * 7 * 86400 +));
You're subtracting 4 * 7 days (ie. 4 weeks), rather than 30 days.

Does that help?

say  substr+lc crypt(qw $i3 SI$),4,5


Comment on Re: Find 30 days from today's date
Select or Download Code
Re^2: Find 30 days from today's date
by CountZero (Bishop) on Jun 08, 2013 at 11:32 UTC
    4 weeks, one month and 30 days are all different. And then we are not even looking at the impact of such strange beasts as leap days, leap seconds, timezones, daylight saving time and more of this kind of "fun".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-12-19 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls