I’m trying to display a date/time in 12-hour format. According to the
Time.strftime API (class Time - RDoc Documentation)
the 12-hour hour range is supposed to be [1…12]. Why am I getting
“00:13:14 PM” for the time portion when I execute the following line?
How should I write so that I’ll get “12:13:14 PM” instead?
Time.local(2007, ‘nov’, 27, 12, 13, 14).strftime(“%Y-%m-%d %I:%M:%S %p”)
Stoopidboi Stoopidboi wrote:
I’m trying to display a date/time in 12-hour format. According to the
Time.strftime API (class Time - RDoc Documentation)
the 12-hour hour range is supposed to be [1…12]. Why am I getting
“00:13:14 PM” for the time portion when I execute the following line?
How should I write so that I’ll get “12:13:14 PM” instead?
Time.local(2007, ‘nov’, 27, 12, 13, 14).strftime(“%Y-%m-%d %I:%M:%S %p”)
I copy-pasted this into IRB and it worked just fine:
irb(main):001:0> Time.local(2007, ‘nov’, 27, 12, 13,
14).strftime(“%Y-%m-%d %I:%M:%S %p”)
=> “2007-11-27 12:13:14 PM”
ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-linux]
-Justin
As another quick note…
irb(main):010:0> Time.local(2007, ‘nov’, 27, 4, 13, 14).strftime("%Y-
%m-%d %I:%M:%S %p")
=> “2007-11-27 04:13:14 AM”
irb(main):009:0> Time.local(2007, ‘nov’, 27, 4, 13, 14).strftime("%Y-
%m-%d %l:%M:%S %p")
=> “2007-11-27 4:13:14 AM”
Using %l instead of %I for hours drops the leading zero.
Sorry to waste everyone’s time, was a JRuby bug. Just found that it has
been fixed in latest trunk.