http://qs321.pair.com?node_id=1231117

mitabrev has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I need a little help.For better understanding my code and output:

my $start_of_week = DateTime->today() ->truncate( to => 'week' ); for ( 0..6 ) { $teden = $start_of_week->clone()->add( days => $_ ); print $teden->mdy."\n"; }

Output:

03-11-2019 03-12-2019 03-13-2019 03-14-2019 03-15-2019 03-16-2019 03-17-2019

What I would like is:

03-11-19 03-12-19 03-13-19 03-14-19 03-15-19 03-16-19 03-17-19

Is this doable with DateTime?

thanks again!

Replies are listed 'Best First'.
Re: Two digit year with DateTime?
by haukex (Archbishop) on Mar 11, 2019 at 11:49 UTC

    I would recommend against a two-digit year, but if you must, see DateTime's strftime method:

    use warnings; use strict; use DateTime; my $start_of_week = DateTime->today()->truncate( to => 'week' ); for ( 0..6 ) { my $teden = $start_of_week->clone()->add( days => $_ ); print $teden->strftime('%m-%d-%y'), "\n"; } __END__ 03-11-19 03-12-19 03-13-19 03-14-19 03-15-19 03-16-19 03-17-19
Re: Two digit year with DateTime?
by hdb (Monsignor) on Mar 11, 2019 at 13:34 UTC

    DateTime lets you specify a formatter, which is used whenever you print or stringify a DateTime object:

    use strict; use warnings; use DateTime; use DateTime::Format::Strptime; my $start_of_week = DateTime->today() ->truncate( to => 'week' ); $start_of_week->set_formatter( DateTime::Format::Strptime->new(pattern + => "%d-%m-%y") ); for ( 0..6 ) { my $teden = $start_of_week->clone()->add( days => $_ ); print "$teden\n"; }

    This is useful when you print in many places of your code and want the same format. Remark: installing DateTime::Format::Strptime unleashed a cascade of dependencies on my Perl install...

Re: Two digit year with DateTime?
by thanos1983 (Parson) on Mar 11, 2019 at 11:52 UTC

    Hello mitabrev,

    In case you do not mind using another module Date::Manip see bellow below:

    #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; # my $tz = new Date::Manip::TZ; say UnixDate(DateCalc( ParseDate('yesterday'), $_ . " days later") , "%m-%d-%y") for (1..7); __END__ $ perl test.pl 03-11-19 03-12-19 03-13-19 03-14-19 03-15-19 03-16-19 03-17-19

    Update: Removing unnecessary line of code.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Perfect! Thanks!
Re: Two digit year with DateTime?
by johngg (Canon) on Mar 11, 2019 at 18:30 UTC

    Time::Piece has been a core module since at least 5.10 so can save you having to install extra modules.

    johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MTime::Piece -E +' my $tp = localtime(); say qq{2-digit year - }, $tp->yy();' 2-digit year - 19

    I hope this is helpful.

    Cheers,

    JohnGG

      This is how dumb I am: I first read the “19” as being the leading two digits of the date and wondered why the library did that and why you were posting broken code… :|

        A person has to look for the matching single quote marks with many of johngg's replies. They are technically one-liners but legible perl on the terminal and effective as sscce.

Re: Two digit year with DateTime?
by karlgoethebier (Abbot) on Mar 11, 2019 at 17:46 UTC

    Two numbers for the year is a bad idea. And to avoid the monster you may take a look at DateTime::Tiny. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Two digit year with DateTime?
by ikegami (Patriarch) on Mar 12, 2019 at 00:24 UTC

    As an aside, DateTime->today() very likely incorrect because it doesn't return the local date. If you want the local date, you could use

    DateTime->today( time_zone => 'local' )

    However, that will fail on a day without midnight. The following is the correct approach is:

    DateTime ->now( time_zone => 'local' ) ->set_time_zone('floating') ->truncate( to => 'day' )
Re: Two digit year with DateTime?
by misc (Friar) on Mar 13, 2019 at 00:07 UTC

    So, that's the well known Year2029 Bug.
    Oh. I see. It will even repeat from now on in ten years, every day.
    (Not like the Year 2000-Bug.)

    But anyways, you don't write what you are trying to do,
    so here comes some general advice.

    And I don't know, which level you are in programming,
    so my 2 cents are even more unspecified.

    I'd try to accomplish things as simple as possible.
    Reading your question, I immediately thought of a regex,
    next thought was grep/map.

    If you know these tools, you can accomplish much more
    than only changing the digits of a date.

    And even when you decide other, (hopefully)
    and use the date in another format,
    you've learned something valuable.

    Best wishes, Michael

Re: Two digit year with DateTime?
by Anonymous Monk on Mar 12, 2019 at 01:14 UTC

    Pox on you for using 2-digit year. If you are being forced to do that, do pass the pox along.