views:

152

answers:

3

I'm using this line to get the beginning time of the first day of the month.

t = Time.now.to_date.beginning_of_month.beginning_of_day

When i display this using t.strftime("%A %b %e @ %l:%m %p")

it shows:

Monday Feb 1 @ 12:02 AM   

The hour is always 12 (instead of 00), and more wierd the minute changes to match the month in integers. For the February date, it shows 12:02 AM I use .prior_month and .next_month on the variable to move forward or backwards in time. So when I move to June, this would display as

Tuesday June 1 @ 12:06 AM

But when I just show the value of t using a straight t.to_s, I get this time of 00:00:00, which is what I expect:

Mon Feb 01 00:00:00 -0700 2010 

A similar error occurs using end_of_day, but the hour is always 11 PM and the minute is the same integer value that matches the month in integers, i.e the time is 11:06 PM in June, 11:02 PM in February.

Quirky? Admittedly a noob to Rails.

Thanks for any comments or explanations.

+2  A: 

%m is month of the year. You want %M, as in t.strftime("%A %b %e @ %l:%M %p").

The end_of_day issue makes sense then, because end of day is 11:59.

Ben
*11:59* is also a great song by *Blondie*, and thanks to this post it started playing in my head.
Arkku
A: 

If we're talking PHP date formatting syntax here, %I refers to a two-digit representation of the hour in 12-hour format, i.e., 01 through 12. If you want 24-hour time, use %H instead.

And %m is months, while %M is minutes.

See the PHP documentation here.

Edit:

I am foolish, and thought you were talking about PHP because of the name of the function. Fortunately, the syntax for this in Ruby appears to be substantially the same, as you can see from j.'s answer.

Syntactic
@Syntactic, he says he's using Ruby on Rails.
macek
Whoops, missed that. It didn't have the ruby tag when I answered.
Syntactic
A: 

I believe this is what you want:

t = Time.now.to_date.beginning_of_month.beginning_of_day
t.strftime("%A %b %e @ %H:%M %p")

You can find more info about time formatting here.

j.