Date comparison..

C

clearguy02

Hi folks,

I need to convert a date from the original format 2007-05-27 00:00:00
to 5/27/2007.
From the original date, I should strip off all the trailing stuff
(00:00:00) and the months (03, 04, 05 etc) should appear as 3, 4, 5
etc.

I am trying to use the sprintf function and some truncating stuff, but
I am not getting the desired results.

Can some one tell me how I can achieve the desired result?

Thanks
-J
 
C

clearguy02

Hi folks,

I need to convert a date from the original format 2007-05-27 00:00:00
to 5/27/2007.


(00:00:00) and the months (03, 04, 05 etc) should appear as 3, 4, 5
etc.

I am trying to use the sprintf function and some truncating stuff, but
I am not getting the desired results.

Can some one tell me how I can achieve the desired result?

Thanks
-J

Sorry for fogetting to post my code:

+++++++++++++++++++++

$date1 = "2007-05-27 00:00:00";
$date1 =~ s/(.*)\s+(.*)/($1)($2)/;
$date2 = $1;
$date2 =~ s/(.*)-(.*)-(.*)/$1$2$3/;
$year = $1;
$month = $2;
$date = $3;
$month =~ s/0//;
$date3 = "$month/$date/$year";
print "$date3\n";
++++++++++++++++++++++++++++

Is there a shorter way to do it?

Thanks
-J
 
T

Tim S

Sorry for fogetting to post my code:

+++++++++++++++++++++

$date1 = "2007-05-27 00:00:00";
$date1 =~ s/(.*)\s+(.*)/($1)($2)/;
$date2 = $1;
$date2 =~ s/(.*)-(.*)-(.*)/$1$2$3/;
$year = $1;
$month = $2;
$date = $3;
$month =~ s/0//;
$date3 = "$month/$date/$year";
print "$date3\n";
++++++++++++++++++++++++++++

Is there a shorter way to do it?

With your code format, this would be the same but more concise:

my $date1 = "2007-05-27 00:00:00";
my ($year, $month, $day) = ($date1 =~ m/^(\d+)-(\d+)-(\d+)\s/) or
die "[$date1] Doesn't look like a date";
printf "%d/%d/%d", $month, $day, $year;

Note you must use "or" not "||" as the precedence is different.

Date::Manip may be of interest too.

HTH

Tim
 
T

Tad McClellan

Sorry for fogetting to post my code:


$date1 = "2007-05-27 00:00:00";
$date1 =~ s/(.*)\s+(.*)/($1)($2)/;
$date2 = $1;


You should never use the dollar-digit variables unless you
have first tested to ensure that the match succeeded.

$date2 =~ s/(.*)-(.*)-(.*)/$1$2$3/;
$year = $1;
$month = $2;
$date = $3;
$month =~ s/0//;
$date3 = "$month/$date/$year";
print "$date3\n";
++++++++++++++++++++++++++++

Is there a shorter way to do it?


$date1 =~ s#(\d+)-0*(\d+)-0*(\d+).*#$2/$3/$1#;
or
$date1 = join '/', ($date1 =~ /0*(\d+)/g)[1,2,0]; # list slice
 
C

clearguy02

Sorry for fogetting to post my code:
$date1 = "2007-05-27 00:00:00";
$date1 =~ s/(.*)\s+(.*)/($1)($2)/;
$date2 = $1;

You should never use the dollar-digit variables unless you
have first tested to ensure that the match succeeded.
$date2 =~ s/(.*)-(.*)-(.*)/$1$2$3/;
$year = $1;
$month = $2;
$date = $3;
$month =~ s/0//;
$date3 = "$month/$date/$year";
print "$date3\n";
++++++++++++++++++++++++++++
Is there a shorter way to do it?

$date1 =~ s#(\d+)-0*(\d+)-0*(\d+).*#$2/$3/$1#;
or
$date1 = join '/', ($date1 =~ /0*(\d+)/g)[1,2,0]; # list slice

Thanks to all of you. Now I realize that I need to compare two dates
(example: 3/18/2007 and 2007-03-18 11.23.54) to see if they are equal
or not from the date comparison point of view, not from the string
comparison point.

Can any one tell me how I can do it?
 
T

Tad McClellan

I need to compare two dates
(example: 3/18/2007 and 2007-03-18 11.23.54) to see if they are equal
or not from the date comparison point of view, not from the string
comparison point.

Can any one tell me how I can do it?


If you normalize to a carefully thought out form, then
you _can_ use the string comparison point of view.

-----------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $date1 = '2007-03-18 11.23.54';
my $date2 = '3/18/2007';

$date1 =~ s/ .*//;
$date2 =~ s#(\d+)/(\d+)/(\d+)# sprintf '%4d-%02d-%02d', $3, $1, $2#e;

print "they are equal\n" if $date1 eq $date2;
 
C

Charlton Wilbur

cg> Is there a shorter way to do it?

use DateTime;
use DateTime::Format::Strptime;

my $date = new DateTime::Format::Strptime (
pattern => '%Y-%m-%d %H:%M:%S' );
my $ouput = $date->mdy('/');

As a general rule, whenever you're doing any sort of date
manipulation, you save yourself time in the long run by using date and
time modules.

Charlton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top