The Man The Man - 6 months ago 11
Perl Question

Substract 15 Minutes from time using Perl

I thought this was going to be very simple but I am really out of options now. I want to substract 15 minutes from a given time.

Example
My time is 15:04 I want to substract 15 minutes to be 14:49. I have searched for solutions on the internet but there is no perl module that can help me out.

Answer

You can use DateTime:

my $dt = DateTime->new(
    year   => 1,
    month  => 1,
    day    => 1,
    hour   => 15, 
    minute => 4,
);  

$dt->subtract(minutes => 15);
printf "%d:%d\n", $dt->hour, $dt->minute; # prints 14:49
Comments