Time.strftime 12 hour format

  • Thread starter Stoopidboi Stoopidboi
  • Start date
S

Stoopidboi Stoopidboi

I'm trying to display a date/time in 12-hour format. According to the
Time.strftime API (http://ruby-doc.org/core/classes/Time.html#M000297)
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")
 
J

Justin Collins

Stoopidboi said:
I'm trying to display a date/time in 12-hour format. According to the
Time.strftime API (http://ruby-doc.org/core/classes/Time.html#M000297)
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
 
J

jacob.dunphy

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.


Stoopidboi said:
I'm trying to display a date/time in 12-hour format. According to the
Time.strftime API (http://ruby-doc.org/core/classes/Time.html#M000297)
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
 
S

Stoopidboi Stoopidboi

Sorry to waste everyone's time, was a JRuby bug. Just found that it has
been fixed in latest trunk.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top