469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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
Dec 26 '07 #1
9 7016
numberwhun
3,503 Expert Mod 2GB
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
Dec 26 '07 #2
rajiv07
141 100+
You can use Date::Calc module to find the difference between two dates.

Here is the Link Date::Calc .

Regards
Rajiv
Dec 26 '07 #3
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..Aftrewhich I will be able to do the rest of processing...
Dec 27 '07 #4
eWish
971 Expert 512MB
Please post you code that you have tried and indicate where you are having you problems.

--Kevin
Dec 27 '07 #5
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $date1 = 361535725;    # in seconds
  3. $date2 = 96201950;     # in seconds
  4. $difference = $date1 - $date2;
  5. print "There were $difference seconds between date1 and date2 \n";
  6. $seconds    =  $difference % 60;
  7. $difference = ($difference - $seconds) / 60;
  8. $minutes    =  $difference % 60;
  9. $difference = ($difference - $minutes) / 60;
  10. $hours      =  $difference % 24;
  11. $difference = ($difference - $hours)   / 24;
  12. $days       =  $difference % 7;
  13. $weeks      = ($difference - $days)    /  7;
  14. print "There are $weeks weeks $days days $hours hours $minutes minutes $seconds seconds\n";
  15.  
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..
Dec 27 '07 #6
KevinADC
4,059 Expert 2GB
pass dates as arguments:

perl yourscript.pl date1 date2

get dates:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my ($date1,$date2) = @ARGV;
  5.  
use the Time::Local module to do the conversion to epoch seconds.
Dec 27 '07 #7
Kevin,

The solution is not clear..Please can you elaborate more..
Dec 27 '07 #8
mehj123
55
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

Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my ($date1,$date2) = @ARGV;
  5.  
  6.  

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
Expand|Select|Wrap|Line Numbers
  1. ($year,$mon, $date) = split/\W+/, $date1;
  2. $epoch_time = timelocal($date,$month,$year);
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.
Dec 27 '07 #9
KevinADC
4,059 Expert 2GB
Kevin,

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

Are you a student? Is this school/class/course work?
Dec 27 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Richard Hollenbeck | last post: by
28 posts views Thread by Steve | last post: by
5 posts views Thread by Dennis M. Marks | last post: by
30 posts views Thread by Dr John Stockton | last post: by
1 post views Thread by Jama | last post: by
5 posts views Thread by Geoff Jones | last post: by
13 posts views Thread by priyasmita_guha | last post: by
8 posts views Thread by Charlie Brookhart | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.