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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding days to a date. 2

Status
Not open for further replies.

vasilek

Programmer
Jan 5, 2003
99
US
How to add a certain number of days to a date. For example I need to add 7 days to 02/01/2003 and get 02/08/2003. Thanks.
 
There's a number of different ways to do all sorts of date conversions (in fact, someone more versed should make a big FAQ desribing what you can do with all of them). If your goal is as simple as your post, then something like this should work:
Code:
my $date = '02/01/2003';
my $newdate = "";
my $numdays = 7;

if($date =~ m~(\d{2})/(\d{2})/(\d{4})~)
{
	$newdate = sprintf("%02d/%02d/%04d",$1,($2+$numdays),$3);
}

print "$date => $newdate\n";
It just matches two numbers slash two numbers slash four numbers, then adds numdays to the second pair, using the sprintf to preserve the leading zeros. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Will it aslo change the month/year respectively if the 7th day falls on the next month/year?

Examples:
01/31/2003 - 02/7/2003
12/28/2003 - 01/04/2004

Does it know that some months have 28(29),30 ann 31 days?
 
You can use Time::parseDate and Time::CTime to do it in a couple of ways (at least):

[tt]use Time::parseDate;
use Time::CTime;

my $date = "02/01/2003";
my $numdays = 7;

my $time = parsedate($date);

# add $numdays worth of seconds
my $newtime = $time + ($numdays * 24 * 60 * 60);

my $newdate = strftime("%m/%d/%Y",
localtime($newtime));
[/tt]

Alternately:
[tt]use Time::parseDate;
use Time::CTime;

my $date = "02/01/2003";
my $numdays = 7;

my $newtime = parsedate("+ $numdays day", NOW =>
parsedate($date));

my $newdate = strftime("%m/%d/%Y",
localtime($newtime));
[/tt]
 
My system says: "Can't locate Time/ParseDate.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .)
 
Perhaps someone will have a way that won't use these modules, but you can install them if not.

If you're using ActiveState perl, ppm install Time::parseDate from a DOS prompt should work.
 
NOw I get this:

C:\>ppm install Time::parseDate
Installing package 'Time-ParseDate'...
Error installing package 'Time-ParseDate': Could not locate a PPD file for package Time-ParseDate
 
Hm. Actually, I used: pm3 install Time::parseDate which worked. (I'm using a recent build of ActiveState perl 5.6.1.)
 
was able to install using - ppm3 install Time::parseDate
 
The code you provided worked perfectly. Thanks a lot.

My hosting provided doesn't have Time::parseDate installed. Gonna talk to them right now.
 
Dates & times are stored as the number of seconds since - I forget, but a while ago.

You can add the required number of seconds to a date/time value to get some date/time in the future.

$secs_in_day = (3600 * 24); # seconds in hour * 24
$now = time();
$two_days_hence = $now + (2 * $secs_in_day);

For folks (like me) who can't be bothered installing modules :) Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Mike, that works for a date some days from today, but how would you do something similar for some days from an arbitrary date?
 
Mike, the using your code I get the future date not in date format.

For example 7 days from today (2/1/2003) is 172802.

How to format it into a date?
 
Rosenk - You'd have to use something to get a time(2) value from a human readable date, Time::Local would seem to be a good bet.

The two functions from that module timelocal() and timegm() do the opposite of what the Perl builtin functions localtime() and gmtime() do. Once you have a time(2) value (seconds since the start of 1st Jan 1970) you can do relative arithmetic.

Vasilek -- how are you getting that number? Looks wierd. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Mike,

$now = time();
$secs_in_day = (3600 * 24); # seconds in hour * 24
$two_days_hence = $now + (2 * $secs_in_day);
print "$two_days_hence\n";

gives me 1044316530



 
$now = time();
$secs_in_day = (3600 * 24); # seconds in hour * 24
$two_days_hence = $now + (2 * $secs_in_day);
print "$two_days_hence\n";

# now convert to a human readable form
print scalar localtime($two_days_hence), "\n";
Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 

use Date::Calc qw:)all);

($new_year, $new_month, $new_day) = Add_Delta_Days($year, $month, $day, $Dd);

... where $new_year,... etc are the results, $year, $month, $day is your arbitrary starting date, and $Dd is the "delta" (the number of days difference). $Dd can be either positive or negative.

I.e.: Add_Delta_Days(2003, 1, 1, 7) would return (2003, 1, 8)

and

Add_Delta_Days(2003, 1, 1, -7) would return (2002, 12, 25)

Cheers,
CM
 
Mike,

I'd like to convert human readable format (Tue Feb 4 02:29:50 2003) to 2/4/2003. How to do that?
 
$now = time();
$secs_in_day = (3600 * 24); # seconds in hour * 24
$two_days_hence = $now + (2 * $secs_in_day);
print "epoch: $two_days_hence\n";
print "human readable: ", scalar localtime($two_days_hence), "\n";

($sec,$min,$hour,$mday,$mon,
$year,$wday,$yday,$isdst) = localtime($two_days_hence);

$mon++;
$year += 1900;

print "formatted: $mon/$mday/$year\n";
Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top