Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date difference question

Status
Not open for further replies.

majorbiff

Programmer
Mar 8, 2005
53
0
0
AU
Hello all.
Putting it simply, I need to find the number of days between two dates and do something if the days are < 30 and something else if days >= 30.
The 'something' my script has to do is not important at the moment.
I have been playing around with a few ideas but have been somewhat overwhelmed by the number of Date manipulation modules on CPAN!

Having more time I probably would have been able to pick the best suited to my requirements.
Time::Interval seems to be a good pick, however there seems to be quite a lot of manipulation that has to be done on the input and returned data.

Has anyone used this module for a similar task?
Can anyone suggest a different module that would be simpler/quicker to use?

Thanks,
Lachlan.

Alchemy is easy with Perl!
s/lead/gold/g;
 
I haven't used Time::Interval, but there are a number of ways to approach this - what format are the dates in?

You could use a method similar to the one in thread219-1176499. Convert both dates to epoch seconds and find the difference. If the difference is greater than the number of seconds in 30 days (30*24*60^2 = 2592000) then you can do whatever actions you require.
 
At the moment, the date format is DD/MM/YYYY.
I was intitially thinking of avoiding the date format all together and just storing epoch time.
When a user makes a second entry (the other date to compare), the current epoch time is noted and the difference between the two values then converted into number of days.
This would enable me to avoid any complex Date/Time modules.
Now that you have mentioned the method, I think ill put it back into the script.
I was initially having problems with the difference -> coversion to days part. Turns out that my calculations for the number of secs in a day were wrong. Ooops!

Thanks for the reply.

Alchemy is easy with Perl!
s/lead/gold/g;
 
Since you mentioned it, I thought I'd give Time::Interval a shot. The easiest way seems to be to convert DD/MM/YYYY to MM/DD/YYYY 00:00 YYYY.
Code:
use Time::Interval;
my @input = ('17/01/2006', '17/02/2006');
# Convert DD/MM/YYYY format to MM/DD/YYYY 00:00 YYYY
my @dates = map { my @temp = split('/', $_);
                  my $str = join('/', @temp[1,0,2]);
                  $str.= " 00:00 $temp[2]"; } @input;
my $data = Time::Interval::getInterval(@dates);
print "Num days: ", $data->{days}, "\n";
Since you already have the time in epoch seconds, I'd stick with making the calculations based on those.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top