my $today = DateTime->today;
my $last = DateTime->last_day_of_month(
  year => $today->year,
  month => $today->month
);

my $diff = $last - $today;

# using DateTime::Format::Duration
my $fmt = DateTime::Format:::Duration->new(
  pattern => "%j days left"
);
print $fmt->format_duration($diff), "\n"

# since this is within the same month, you could
# use in_units as well

print $diff->in_units('days'), " days left\n";

--d

Jay wrote:
How do I calculate the number of days left in the month?

The best I could do was something like this, which doesn't really work:

my $now                 = DateTime->now();
my $first_of_this_month = DateTime->new(
   month   => $now->month(),
   day     => 1,
   year    => $now->year(),
);

my $one_month = DateTime::Duration->new( months => 1 );
my $one_day   = DateTime::Duration->new( days   => 1 );
my $last_day_of_this_month = $first_of_this_month + $one_month - $one_day;

my $d = DateTime::Format::Duration->new( pattern => '%e days left in
this month' );
print $d->format_duration( $now - $last_day_of_this_month ), "\n";


Thanks,
Jay


Reply via email to