Perl Date Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nzsquall
    New Member
    • Oct 2009
    • 22

    Perl Date Function

    Hi Code Masters,

    I have a function to calculate current date as
    Code:
    sub currentDate {
        my @timeParts=localtime;
        my ($day, $month, $year) = ($timeParts[3],$timeParts[4],$timeParts[5]);
        return ($month+1)."/".$day."/".($year+1900);
    }
    How do I calculate two months after today's date?

    Many thanks.
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    Date::Calc

    Code:
    use strict;
    use warnings;
    use Date::Calc qw(Today Add_Delta_YMD Date_to_Text);
    
    my @today = Today();
    my @date = Add_Delta_YMD(@today, 0,2,0);
    print "2 months from now will be: ", Date_to_Text(@date), "\n";
    
    __END__
    
    2 months from now will be: Thu 28-Oct-2010

    Comment

    • Nzsquall
      New Member
      • Oct 2009
      • 22

      #3
      Cool, thanks very much.

      Comment

      • Nzsquall
        New Member
        • Oct 2009
        • 22

        #4
        Is there a way that I can get rid of the Thu to simply display as "28-Oct-2010"?

        Comment

        • toolic
          Recognized Expert New Member
          • Sep 2009
          • 70

          #5
          Read the documentation. The return value of the Date_to_Text function is clearly specified:

          Code:
          my @today = Today();
          my @date = Add_Delta_YMD(@today, 0,2,0);
          my $date_fmt = (split /\s+/, Date_to_Text(@date))[1];
          print "2 months from now will be: $date_fmt\n";

          Comment

          • Nzsquall
            New Member
            • Oct 2009
            • 22

            #6
            I really appreciate this, sorry for the laziness this time. I will try to do it myself the next time, thanks.

            Comment

            Working...