Convert string into incremental date

D

djray

My question is two part:
1. I am reading a date from a file in the form mm/dd/yy (i.e.
05/30/06). I need to convert that string into a date.
2. I need to be able to increment that date:
Example:
$date = 05/30/06;
$date = $date + 1; ($date = 05/31/06)
$date = $date + 1; ($date = 06/01/06)

Any help would be greatly appreciated.

-Ray
 
I

it_says_BALLS_on_your forehead

djray said:
My question is two part:
1. I am reading a date from a file in the form mm/dd/yy (i.e.
05/30/06). I need to convert that string into a date.
2. I need to be able to increment that date:
Example:
$date = 05/30/06;
$date = $date + 1; ($date = 05/31/06)
$date = $date + 1; ($date = 06/01/06)

Any help would be greatly appreciated.

See the Add_Delta_YMD function.

http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod
 
P

Paul Lalli

djray said:
My question is two part:
1. I am reading a date from a file in the form mm/dd/yy (i.e.
05/30/06). I need to convert that string into a date.
2. I need to be able to increment that date:
Example:
$date = 05/30/06;
$date = $date + 1; ($date = 05/31/06)
$date = $date + 1; ($date = 06/01/06)

Any help would be greatly appreciated.

There are probably 10 different modules on CPAN that can help you with
this. Have you looked there yet? http://search.cpan.org search for
"date" and/or "time".

If your data really is that structured, however, you might not need to
bother with a CPAN module. Parse out the three numbers using a regular
expression (see perldoc perlre), convert them to seconds since the
epoch using Time::Local's timelocal() (see perldoc Time::Local), and
add one day (24 * 60 * 60), and convert to a string of your choosing
using POSIX's strftime (see `man strftime`) and localtime() (see
perldoc -f localtime).

(standard warnings about daylight savings time apply...)

Hope this helps,
Paul Lalli
 
M

Mothra

djray said:
My question is two part:
1. I am reading a date from a file in the form mm/dd/yy (i.e.
05/30/06). I need to convert that string into a date.
2. I need to be able to increment that date:
Example:
$date = 05/30/06;
$date = $date + 1; ($date = 05/31/06)
$date = $date + 1; ($date = 06/01/06)

Any help would be greatly appreciated.

-Ray

This might get you started :)

use strict;
use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
pattern => '%m/%d/%y',
time_zone => 'GMT',
);
my $dur = DateTime::Duration->new( days => 1 );

while (<DATA>) {
chomp;
my $dt = $Strp->parse_datetime($_);
print $dt + $dur;
}
__DATA__
05/31/06
06/01/06


I hope this helps

Mothra
 
U

usenet

djray said:
My question is two part:
1. I am reading a date from a file in the form mm/dd/yy (i.e.
05/30/06). I need to convert that string into a date.

Let a module do that for you.
2. I need to be able to increment that date:

Let a module do that for you as well.

#!/usr/bin/perl

use Date::Manip;

my $date = "05/30/06";

$date = DateCalc($date, "+ 1 day");
$date = DateCalc($date, "+ 1 day");

print UnixDate($date, "%D"); #display like mm/dd/yy

__END__
 
D

DJ Stunks

Mothra said:
This might get you started :)

use strict;
use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
pattern => '%m/%d/%y',
time_zone => 'GMT',
);
my $dur = DateTime::Duration->new( days => 1 );

while (<DATA>) {
chomp;
my $dt = $Strp->parse_datetime($_);
print $dt + $dur;
}
__DATA__
05/31/06
06/01/06


I hope this helps

it helps me!

thanks,
-jp
 
R

rjulich

Thank you to everyone that helped me out with this. I tried them all,
but David's was the simplest to implement. Thanks again.

-Ray
 
U

usenet

Thank you to everyone that helped me out with this. I tried them all,
but David's was the simplest to implement.

I'm glad you liked my solution. I must point out, however, that
simplicity (for the user/programmer) often comes at the cost of
efficiency (for the machine), and my solution is probably the least
efficient of those offered. But, unless you're crunching thousands of
dates, it probably doesn't really matter on modern hardware.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top