Is there a strftime format I’m missing that would get a time to
display as 8:30 PM instead of 08:30 PM? Using @mytime.strftime(" %I:%M
%p") gives me a display like 08:30 PM, but I’d like it to show as h:mm
and not hh:mm for times earlier than 10:00. Thanks.
Sharon M. wrote:
Is there a strftime format I’m missing that would get a time to
display as 8:30 PM instead of 08:30 PM? Using @mytime.strftime(" %I:%M
%p") gives me a display like 08:30 PM, but I’d like it to show as h:mm
and not hh:mm for times earlier than 10:00. Thanks.
Funnily enough, my colleague just blogged about this…
On Nov 27, 2007 2:25 PM, Tom T. [email protected]
wrote:
Funnily enough, my colleague just blogged about this…
Infovore » Displaying the number of hours without a leading zero in Ruby’s #strftime
I guess it’s fairly popular:
WordPress
Error establishing a database connection
–
Greg D.
http://destiney.com/
Sharon M. wrote:
Is there a strftime format I’m missing that would get a time to
display as 8:30 PM instead of 08:30 PM? Using @mytime.strftime(" %I:%M
%p") gives me a display like 08:30 PM, but I’d like it to show as h:mm
and not hh:mm for times earlier than 10:00. Thanks.
Well I’m not using strftime and it is a little long winded but I do have
code that does what you ask:
<%= Time.now.hour < 10 ? Time.now.hour + ’ AM’ : Time.now.hour.to_i > 12
? (Time.now.hour.to_i - 12).to_s + ’ PM’ : Time.now.hour + ’ PM’ %>
Hope this helps,
-S
On Nov 27, 2007 9:03 AM, Sharon M. [email protected] wrote:
Is there a strftime format I’m missing that would get a time to
display as 8:30 PM instead of 08:30 PM? Using @mytime.strftime(" %I:%M
%p") gives me a display like 08:30 PM, but I’d like it to show as h:mm
and not hh:mm for times earlier than 10:00. Thanks.
Time#strftime uses the platform function with the same name, this
might be platform dependent but on both OSX and Ubuntu linux,
@mytime.strftime(“%l:%M %p”) #=> " 8:30 pm"
Note that that first format character is a lowercase L not an uppercase
I.
Also lowercase k will do the same thing but use military time.
Note that this suppresses the leading zero, but does put a blank in
it’s place. If you really want to get write of the leading blank,
just do:
@mytime.strftime(“%l:%M %p”).strip
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Rick DeNatale wrote:
Note that that first format character is a lowercase L not an uppercase I.
You can do:
@mytime.strftime("%I:%M %p").sub(/^0/,’’)
This should work on all platforms I think.
Cheers,
Gary.
That works on the Windows system I’m developing on as well as my Linux
host. Thanks!
@mytime.strftime("%I:%M %p") alone didn’t show the hour at all on a
Windows system.