Anyway, I need to take today’s date and add 45 days to it. If that 45th day is after the 25th of any given month, I need to “round up” to the 1st of the next month.
I’m just starting to write some code now, so I don’t have something for you to look at. Mostly, I’m hoping I don’t have to re-invent the wheel here.
This is for an insurance quoting app, btw. Not homework.
Here’s what I have so far. So, when the $day is greater than 25, I could increment the $month and set $day to 1, but then I’d have to be careful of December. So, if after adding 1 to $month, if it is greater than 12, set it to 1 and increment the year.
Am I making this harder than it needs to be or am I on the right track?
I’m glad that you got it working. Do you have any aversion to me using it?
I would modify it for dates in the past.
You noted some minor changes . Do you have the completed (and final) code?
Here’s the final code, but its not significantly different then what I posted. I removed my print statements. Those were just for testing. At the end, I assign the date to variable that used later in the full script.
I wonder if anyone on this forum actually knows Perl?
Add 45 days to the current date, and if the result is the 25th or later of a month, skip to the 1st of the next month:
use DateTime; # include the DateTime module in our script
my $dt = DateTime->now( time_zone => 'floating' ); # create a DateTime object set to the current date and time, with no particular timezone
$dt->add( days => 45 ); # add 45 days
if ($dt->day >= 25) # see if the date is the 25th or later
{
$dt->set_month( $dt->month + 1 ); # set the month to it's current value plus 1
$dt->set_day( 1 ); # set the day to the 1st
}
Personally I don’t like to post suggestions that require a non-core module unless there is clear benefit, which your code does not provide, it is just a different way to do the same thing.
The clear benefit is that it provides a single point of entry to the data and insures correct calculation of complex date arithmetic, and takes advantage of the expressive power of Perl. The other examples listed here are error prone, unobvious, and make the sleek Ferrari that is Perl look like a jalopy.
Actually I would use excellent Date::Manip module for this…
use Date::Manip;
my $date = “2007-08-20”;
my $date2 = UnixDate(DateCalc($date,“+ 45 days”,\$err),“%Y-%m-%d”);
if (UnixDate($date2,“%d)> 25) {
$enddate = UnixDate(DateCalc($date2,”+ 10 days",\$err),“%Y-%m-”).“01”;
} else {
$enddate = $date2;
}
print "The date you are looking for is “.$enddate.”
";