in reply to date span in YYYYMMDD

Here's an example using Date::Calc.

This code gets the current day, month and year using Date::Calc::Localtime, rather than the built in localtime().

This code will figure out the number of days in each month.
Leap years are handled correctly.

You could always modify this code so that you can get those numbers yourself outside of the subroutine and have the sub just do the calculations.

#!/usr/bin/perl use strict; use warnings; use Date::Calc qw/Localtime Days_in_Month/; sub give_dates { my ($year, $month, $day) = (Localtime())[0,1,2]; my $days_in_mon = Days_in_Month($year, $month); my $cutoff = int($days_in_mon / 2); my ($start, $end); if ( $day <= $cutoff) { # First half of month $start = $year . sprintf("%02d", $month) . '01'; $end = $year . sprintf("%02d", $month) . $cutoff; } else { # Second half of month $start = $year . sprintf("%02d", $month) . $cutoff; $end = $year . sprintf("%02d", $month) . $days_in_mon; } return ($start, $end); } print join " ", give_dates(), "\n";

Cheers.

BazB

Update: Corrected code. Initial version gave dates in wrong format (YYYYMMMDD not YYYYMMDD).


If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.