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

Dear Master Monks,

I currently use:

use POSIX qw(strftime); my $todays_date = strftime '%d.%m.%y', localtime;

Is there a better (shorter) way?

Thanks,
Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: Best way to get DD.MM.YY
by ikegami (Patriarch) on Dec 12, 2005 at 19:20 UTC

    If this is for display purposes, you should probably use %x which returns the date in a locale-dependant format.

    use POSIX qw(strftime); my $todays_date = strftime '%x', localtime;

    For me, it would display 2005/01/02 for January 2nd. For you, it will display 1.2.2005 if you configured your OS to match your preference.

Re: Best way to get DD.MM.YY
by shiza (Hermit) on Dec 12, 2005 at 19:07 UTC
    Using Date::Simple you could do:
    use Date::Simple ('today'); my $today = today(); my $year = $today->year; my $month = $today->month; my $day = $today->day;
    It's a few more lines, but you get the added benefit of having an object and its various methods. It's especially usefull if you're doing date calculations.


    Using Time::Piece you could do:
    use Time::Piece; my $t = localtime; my $todays_date = $t->mdy('.');

Re: Best way to get DD.MM.YY
by ambrus (Abbot) on Dec 12, 2005 at 19:52 UTC

    strftime is probably the simplest solution.

    I like to use Date::Manip as it's an easy-to use date/time module, although not the fastest or most complete one. With it, you can do this:

    use Date::Manip; $todays_date = UnixDate("now", "%d.%m.%y");
Re: Best way to get DD.MM.YY
by BigLug (Chaplain) on Dec 12, 2005 at 23:18 UTC
    The obligatory DateTime solution:
    DateTime->now()->strftime('%d.%m.%y')
    That out of the way, let me say that either DD.MM.YY or MM.DD.YY is a crazy way of dealing with dates. Neither is sortable and the two are not distinguishable for 144 days of the year. An ISO date/time format solves this: YYYY-MM-DDTHH:MM:SS

    However if all you're doing is displaying the date to a known audience who have either asked, or specified that this is the best *display* format, then go ahead.

    I'd still argue however that a two-digit year is fraught with danger .. remember the Y2K bug? This is it!

    Cheers!
    Rick

      Or, better yet:

      my $dmy = DateTime->now()->dmy('.');

      I discovered the mdy() method a few days ago and it's a pretty sweet time-saver. I guess dmy() would be the equivalent outside the US.

      -sam

        Thanks Sam! I'd completely forgotten the dmy & mdy methods!
Re: Best way to get DD.MM.YY
by Celada (Monk) on Dec 12, 2005 at 20:39 UTC

    I apologize in advance for being picky...

    my $todays_date = strftime '%d.%m.%y', localtime;

    Is there a better (shorter) way?

    Maybe not shorter, but certainly better:

    my $todays_date = strftime '%d.%m.%Y', localtime;

    The current year is 2005, not 5. See also Markus Kuhn's excellent ISO 8601 page for en even better format.