Date Difference between two dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vinod11
    New Member
    • Dec 2007
    • 4

    Date Difference between two dates

    Hi,

    I need to find out the difference between two dates and both the dates are provided by the user.
    Please can someone help me on this..Need it urgently !!!

    Ex:
    Date1 : 2007/12/26
    Date2: 2007/12/24
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by Vinod11
    Hi,

    I need to find out the difference between two dates and both the dates are provided by the user.
    Please can someone help me on this..Need it urgently !!!

    Ex:
    Date1 : 2007/12/26
    Date2: 2007/12/24
    Considering this is a learning forum, you won't be provided the code to do this. Instead, can you post what you have tried thus far and we will try and help you get your code working?

    There are a few ways that you can go about this, including converting the dates to epoch time (which is Unix time, time in seconds since January 1st, 1970) and comparing them that way.

    Regards,

    Jeff

    Comment

    • rajiv07
      New Member
      • Jun 2007
      • 141

      #3
      You can use Date::Calc module to find the difference between two dates.

      Here is the Link Date::Calc .

      Regards
      Rajiv

      Comment

      • Vinod11
        New Member
        • Dec 2007
        • 4

        #4
        Thanks all...!!
        I understand that this is a learning forum and I am not looking out for an exact code for doing this.There is a code which I am using which converts two dates in EPOCH seconds ..calculates the difference of bothe the dates i..e todays date wh0ch it gets from LOCALTIME() and a date which is provided in the script.
        However,this does not suffice my requirement as I want the two dates to be provided from command line and then the code should convert the two dates in EPOCH seconds and then do the processing..
        And I want some help to accept bothe the dates and convert it into EPOCH seconds..Aftrew hich I will be able to do the rest of processing...

        Comment

        • eWish
          Recognized Expert Contributor
          • Jul 2007
          • 973

          #5
          Please post you code that you have tried and indicate where you are having you problems.

          --Kevin

          Comment

          • Vinod11
            New Member
            • Dec 2007
            • 4

            #6
            [code=perl]
            #!/usr/bin/perl
            $date1 = 361535725; # in seconds
            $date2 = 96201950; # in seconds
            $difference = $date1 - $date2;
            print "There were $difference seconds between date1 and date2 \n";
            $seconds = $difference % 60;
            $difference = ($difference - $seconds) / 60;
            $minutes = $difference % 60;
            $difference = ($difference - $minutes) / 60;
            $hours = $difference % 24;
            $difference = ($difference - $hours) / 24;
            $days = $difference % 7;
            $weeks = ($difference - $days) / 7;
            print "There are $weeks weeks $days days $hours hours $minutes minutes $seconds seconds\n";
            [/code]

            This is the raw code that I am using.
            Here there are two variables date1 and date2 which are in seconds.
            My requirement is :
            pass dates through command line
            convert those dates in epoch seconds ,
            from where the script will do its processing..
            Last edited by numberwhun; Dec 27 '07, 10:05 AM. Reason: add code tags

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              pass dates as arguments:

              perl yourscript.pl date1 date2

              get dates:

              Code:
              #!usr/bin/perl
              use strict;
              use warnings;
              my ($date1,$date2) = @ARGV;
              use the Time::Local module to do the conversion to epoch seconds.

              Comment

              • Vinod11
                New Member
                • Dec 2007
                • 4

                #8
                Kevin,

                The solution is not clear..Please can you elaborate more..

                Comment

                • mehj123
                  New Member
                  • Aug 2007
                  • 55

                  #9
                  Originally posted by Vinod11
                  Kevin,

                  The solution is not clear..Please can you elaborate more..
                  Hi Vinod,

                  What Kevin is trying to explain is this..

                  Suppose you have the two dates like this Date1 : 2007/12/26 and Date2: 2007/12/26..

                  You pass both these to the script thru command line like this

                  Code:
                  perl scriptname.pl 2007/12/26 2007/12/26
                  And in the script, you can retrieve the same using the array @argv, which is used to manipulate the command line arguments
                  [CODE=perl]#!usr/bin/perl
                  use strict;
                  use warnings;
                  my ($date1,$date2) = @ARGV;

                  [/CODE]


                  Now the variables $date1 and $date1 will have the dates that you have entered thru the command line..

                  Now for converting the same to epoch seconds, use the Time:Local module like this
                  [CODE=perl]
                  ($year,$mon, $date) = split/\W+/, $date1;
                  $epoch_time = timelocal($date ,$month,$year);[/CODE]

                  Now the timelocal() takes the month value as an integer, from 0-11.. so if you have to pass the month "December" you would be passing 11. so you have to modify the $month variable accordingly.

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by Vinod11
                    Kevin,

                    The solution is not clear..Please can you elaborate more..

                    Are you a student? Is this school/class/course work?

                    Comment

                    Working...