# Format for getting time to display as 8:30 PM not 08:30 PM?

**URL:** <https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386>\
**Category:** Rails\
**Created:** [November 27, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386 "2007-11-27T15:04:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [November 27, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/1 "2007-11-27T15:04:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [November 27, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/2 "2007-11-27T21:25:46Z")

</div>

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…

[http://infovore.org/archives/2007/11/27/displaying-the-number-of-hours-without-a-leading-zero-in-rubys-strftime/](http://infovore.org/archives/2007/11/27/displaying-the-number-of-hours-without-a-leading-zero-in-rubys-strftime/)

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [November 27, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/3 "2007-11-27T21:30:24Z")

</div>

On Nov 27, 2007 2:25 PM, Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Funnily enough, my colleague just blogged about this…
> 
> [Infovore » Displaying the number of hours without a leading zero in Ruby’s #strftime](http://infovore.org/archives/2007/11/27/displaying-the-number-of-hours-without-a-leading-zero-in-rubys-strftime/)

I guess it’s fairly popular:

WordPress

Error establishing a database connection

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [November 27, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/4 "2007-11-27T21:49:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [November 27, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/5 "2007-11-27T22:10:23Z")

</div>

On Nov 27, 2007 9:03 AM, Sharon M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [November 27, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/6 "2007-11-27T22:16:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [December 23, 2007, 3:09am UTC](https://www.ruby-forum.com/t/format-for-getting-time-to-display-as-8-30-pm-not-08-30-pm/121386/7 "2007-12-23T03:09:17Z")

</div>

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.
