Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 5th, 2007, 01:05 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default get date difference between two dates in perl

Code:
print "$startday-$startmon-$nyear\n"; # 2-4-2007
print "$endday-$endmon-$nyear\n"; # 9-4-2007
i have two dates in the format above, is there anyway to get the difference between them? the dates will change as its dynamic, so just a classic subtraction won't work. any ideas? examples?

Picco

www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
Reply With Quote
  #2 (permalink)  
Old March 5th, 2007, 05:33 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'd use the Date::Calc module from CPAN.http://search.cpan.org/dist/Date-Calc/Calc.pod

Code:
($y,$m,$d) = Delta_YMD($year1,$month1,$day1, $year2,$month2,$day2);
Cheers

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #3 (permalink)  
Old March 9th, 2007, 11:48 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

that probably does work great, but i don't have Date::Calc installed on the server i'm using and I don't have the authoristion to install it. can it be done another way? maybe using more standard Perl modules?

www.crmpicco.co.uk
www.ie7.com
Reply With Quote
  #4 (permalink)  
Old March 10th, 2007, 07:46 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

ah ok,

Well, if they were the other way round you could just order them as strings - i.e. 2007-4-9 comes after 2007-4-2 in the alphabet. In the following code there is a subroutine to swap the field ordering (reverse_date()). We then use the block form of the sort function to string compare the two dates with their fields reversed.

Code:
#!/usr/bin/perl
use warnings;
use strict;

sub reverse_date($) {
  my $date_to_reverse = shift;
  my @swap = split /-/,$date_to_reverse;
  return join '-',reverse @swap;
}

my $first_date = "9-4-2007";
my $second_date = "2-4-2007";

my ($earlier,$later) = sort {reverse_date($a) cmp reverse_date($b)} $first_date, $second_date;
print "Earlier date: $earlier\nLater date: $later\n";
On my computer this does:

Code:
charlie@mogadon:~$ ./tmp.pl
Earlier date: 2-4-2007
Later date: 9-4-2007
charlie@mogadon:~$
HTH

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #5 (permalink)  
Old March 12th, 2007, 12:06 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

cheers for that Charlie, I was meaning something that would give me the difference between two dates. Say 10 March 2007 and 27 February 2007. Returning 11 days. Taking into consideration the change of month and stuff like that (31 days....28 days....all that nastyness!)

www.crmpicco.co.uk
www.ie7.com
Reply With Quote
  #6 (permalink)  
Old March 12th, 2007, 12:08 PM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Code:
    Earlier date: 2-4-2007
    Later date: 9-4-2007


    It also returns that on my machine......is there any way to modify it to get the difference in days???


www.crmpicco.co.uk
www.ie7.com
Reply With Quote
  #7 (permalink)  
Old March 15th, 2007, 05:43 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Sorry mate, me being dozy. Sounds like what you're actually after is $date1-$date2=$difference_in_days.

You /could/ write something to do this but it'd be a big gnarly thing to write, normally I'd just say use CPAN, but it sounds like you can't. If the dates are in a database you may find that you can do the date maths there and save yourself some faff. Otherwise, take a look at the source of Date::Calc and see how they did it!

Good luck

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #8 (permalink)  
Old March 15th, 2007, 05:46 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Oh or try this:

http://www.unix.org.ua/orelly/perl/cookbook/ch03_06.htm

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #9 (permalink)  
Old March 15th, 2007, 10:52 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Code:
#!/usr/bin/perl

use strict;
use Date::Calc;

my @picco = (1981, 6, 16);      # 16 Jun 1981
my @gill  = (1973, 1, 18);      # 18 Jan 1973
$difference = Delta_Days(@picco, @gill);
print "There were $difference days between Picco and Gill\n";
superb...

www.crmpicco.co.uk
www.ie7.com
Reply With Quote
  #10 (permalink)  
Old April 11th, 2007, 03:44 PM
Registered User
 
Join Date: Apr 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Charlie,

your last post was very helpful. Even along with that, i am looking to change the date format from 01/12/07 to 12-Jan-2007, can you please tell me how can i do that and by using which module.

Thanks,
Hur
Quote:
quote:Originally posted by ciderpunx
 Oh or try this:

http://www.unix.org.ua/orelly/perl/cookbook/ch03_06.htm

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between 2 dates comicghozt Access 5 July 14th, 2006 03:45 AM
Urgent! how to calculate difference between dates Subuana Beginning VB 6 2 March 22nd, 2006 05:34 PM
Difference in Dates mcinar Javascript How-To 0 May 8th, 2005 07:08 PM
Difference between two dates in Vb.net bimal VS.NET 2002/2003 5 August 15th, 2004 11:32 PM
how to calculate difference in dates ashu_gupta75 Excel VBA 3 August 9th, 2004 06:11 AM



All times are GMT -4. The time now is 11:31 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.