in reply to How to get last month date using Date::Calc
Hi, date math is tricky, not so much because there are no tools, but because one must be very precise in thinking about the calculation actually needed.
If you are asking on, say, March 30, what month is a month ago?
Perl tools differ in their answer:
... and that's not even taking into account Daylight Savings Time and other things. So be careful!$ perl -MDate::Calc=Add_Delta_YM -E 'say "$_ : " . join "-", Add_Delta +_YM( split("-", $_), 0,-1) for qw/2018-03-28 2018-03-29 2018-03-30 20 +18-03-31/' 2018-03-28 : 2018-2-28 2018-03-29 : 2018-2-28 2018-03-30 : 2018-2-28 2018-03-31 : 2018-2-28 $ perl -MTime::Piece -E 'do { $t = Time::Piece->strptime($_, "%Y-%m-%d +"); say "$_ : " . $t->add_months(-1)->strftime("%Y-%m-%d") } for qw/2 +018-03-28 2018-03-29 2018-03-30 2018-03-31/' 2018-03-28 : 2018-02-28 2018-03-29 : 2018-03-01 2018-03-30 : 2018-03-02 2018-03-31 : 2018-03-03
(For those wondering, DateTime returns the same as Date::Calc.)
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to get last month date using Date::Calc (caution advised)
by hippo (Archbishop) on Oct 12, 2018 at 12:39 UTC | |
by Anonymous Monk on Oct 12, 2018 at 20:59 UTC |