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

Hi there, I have some finals coming up soon, and I want to make a script that will tell me how many days I have left. For example, if the date of the exam is 05-08-2012 (DD MM YYYY) I want Perl to tell me how many days I have left from whatever day it currently is. I have tried a couple of things, but ended up getting confused and now don't know what to do. UPDATE: Sorry for the late reply, and thanks for the code/updates. I had tried the Date::Calc modules but wasn't getting them to work how I wanted. Thanks.

Replies are listed 'Best First'.
Re: Comparing dates in Perl
by MidLifeXis (Monsignor) on Apr 04, 2012 at 17:14 UTC
    I have tried a couple of things

    Please show what you have tried. You may be close and just need a little push. Include the code in <code>...</code> tags.

    Update: You may wish to peruse Date to see if there is anything that would help.

    --MidLifeXis

Re: Comparing dates in Perl
by dasgar (Priest) on Apr 04, 2012 at 17:15 UTC

    Don't know if it's the "best" way, but I personally would use Date::Calc's Delta_Days function.

Re: Comparing dates in Perl
by Lady_Aleena (Priest) on Apr 04, 2012 at 17:18 UTC

    Look at Delta_Days and Delta_DHMS in the Date::Calc module. That should give you what you need.

    Have a cookie and a very nice day!
    Lady Aleena
Re: Comparing dates in Perl
by ikegami (Patriarch) on Apr 04, 2012 at 19:50 UTC
    use strict; use warnings; use feature qw( say ); use DateTime qw( ); use DateTime::Format::Strptime qw( ); my $format = DateTime::Format::Strptime->new( pattern => '%d-%m-%Y', time_zone => 'local', on_error => 'croak', ); my $target = $format->parse_datetime('05-08-2012'); my $today = DateTime->today( time_zone => 'local' ); say $today->delta_days($target)->in_units('days'); # 123

      ++

      Most of the time, I find myself coming back to this module because it’s an abstract data type.   An instance of the object, once loaded with a particular date by whatever means, “is an” instance of that date.   It is completely opaque and it just does the right thing.   Since you often find yourself doing a series of not-quite trivial things with date/time values, this is a great tool.

      I peeked at the source code once.   Wow.   “Glad somebody wrote that (and wrote it so well) ... glad it wasn’t me.”

      Hey thanks man. Appreciate it.