htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I'm using this:
use DateTime qw( ); my $dt = DateTime->now( time_zone => 'Pacific/Honolulu' ); my $dateadded_time = $dt->strftime("%k:%M"); my $dateadded_date = $dt->strftime("%b %e, %Y");
The problem is %k shows up as 24 time. I'm having trouble figuring out how to get this back to 12 hour time if the hour is more than 12. Thanks!

Replies are listed 'Best First'.
Re: 24 hour to 12 hour using DateTime
by kennethk (Abbot) on Aug 01, 2014 at 05:05 UTC

    As documented in the strftime Patterns section of DateTime, 12-hour time (without a leading zero) corresponds to %l, so

    my $dateadded_time = $dt->strftime("%l:%M");
    or possibly
    my $dateadded_time = $dt->strftime("%l:%M %P");

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thank you so much!