CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Comparing 2 strings with date/time

 



demarcao
New User

Sep 17, 2002, 1:16 PM

Post #1 of 4 (550 views)
Comparing 2 strings with date/time Can't Post

I have the 2 following strings:

17Sep0215382228650366661
and
17Sep02153850

As you see the first part is a date/time I need to be able to tell the time difference between the two.. Any ideas would be great..
thanks!

- Alex


thebitch
User

Sep 17, 2002, 7:29 PM

Post #2 of 4 (548 views)
Re: [demarcao] Comparing 2 strings with date/time [In reply to] Can't Post

so what do you want to compare, the strings or the dates?

The following works

Code
if( '17Sep0215382228650366661' > '17Sep02153850') { 
print "1st is greater";
}elsif( '17Sep0215382228650366661' eq '17Sep02153850') {
print "they're equal";
}else{
print "2nd is greater";
}



demarcao
New User

Sep 18, 2002, 5:20 AM

Post #3 of 4 (542 views)
Re: [thebitch] Comparing 2 strings with date/time [In reply to] Can't Post

OK I'll try to explain better:

17Sep0215382228650366661
Day-Month-Year-time - Randomnumbers

17Sep02153850
Day-Month-Year-time

Where the time ends on the first field needs to be chopped off, and then compared to the string below and I need to know how much time has elapsed. THe top string could change in length, but the first part is always day-month-year-time.

- Alex


davorg
Thaumaturge / Moderator

Sep 18, 2002, 5:50 AM

Post #4 of 4 (541 views)
Re: [demarcao] Comparing 2 strings with date/time [In reply to] Can't Post

Use Time::Piece.

Code
#!/usr/bin/perl -w 

use strict;
use Time::Piece;

my $t1 = '17Sep0215382228650366661';
my $t2 = '17Sep02153850';

$t1 = substr($t1, 0, 13);
$t2 = substr($t2, 0, 13);

my $fmt = '%d%b%y%H%M%S';

$t1 = Time::Piece->strptime($t1, $fmt);
$t2 = Time::Piece->strptime($t2, $fmt);

my $diff = $t2 - $t1;

print "Diff is ", $diff->seconds, " secs\n";


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives