Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/perl
use strict;
use warnings;
use 5.10.0;
{ # First day of next month
use DateTime qw[];
my $d = DateTime->today->truncate(to => 'month')->add(months => 1);
say $d->ymd;
}
{ # First day of next month
use Time::Piece qw[];
my $d = Time::Piece->new;
$d = $d + (86400 * $d->month_last_day) - (86400 * ($d->mday - 1));
say $d->ymd;
}
{ # First day of next month
use Date::Simple qw[];
my $d = Date::Simple->today;
$d = $d + Date::Simple::days_in_month($d->year, $d->month) - ($d->day - 1);
say $d;
}
use Date::Simple qw[days_in_month];
sub mkdate ($$$) {
my ($y, $m, $d) = @_;
if ($m < 1 || $m > 12) {
$y += $m / 12;
$m %= 12;
if ($m < 1) {
$m += 12;
$y--;
}
}
if ($d < 1) {
while ($d < 1) {
$m--;
if ($m < 1) {
$m = 12;
$y--;
}
$d += days_in_month($y, $m);
}
}
elsif ($d > 28) {
my $dim = days_in_month($y, $m);
while ($d > $dim) {
$d -= $dim;
$m++;
if ($m > 12) {
$m = 1;
$y++;
}
$dim = days_in_month($y, $m);
}
}
return ($y, $m, $d);
}
my $d = Date::Simple->today;
# First day of next month
say Date::Simple->new(mkdate($d->year, $d->month + 1, 1));
# Last day of next month
say Date::Simple->new(mkdate($d->year, $d->month + 2, 0));
# First day of previous month
say Date::Simple->new(mkdate($d->year, $d->month - 1, 1));
# Last day of previous month
say Date::Simple->new(mkdate($d->year, $d->month, 0));
# First day of current month
say Date::Simple->new(mkdate($d->year, $d->month, 1));
# Last day of current month
say Date::Simple->new(mkdate($d->year, $d->month + 1, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.