简体   繁体   English

strftime(小时)显示的时间是否错误?

[英]Is strftime (hour) showing wrong time?

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") 当我使用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. 小时始终为12(而不是00),而分钟数的变化更会与整数匹配月份。 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. 对于2月日期,它显示的是12:02 AM我在变量上使用.prior_month.next_month在时间上向前或向后移动。 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: 但是,当我仅使用直线t.to_s显示t的值时,我得到的时间为00:00:00,这是我期望的:

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, ie the time is 11:06 PM in June, 11:02 PM in February. 使用end_of_day会发生类似的错误,但小时始终为11 PM,分钟是与整数匹配的月份的相同整数值,即时间为6月的11:06 PM,2月的11:02 PM。

Quirky? 新奇? Admittedly a noob to Rails. 诚然是Rails的菜鸟。

Thanks for any comments or explanations. 感谢您的任何评论或解释。

%m is month of the year. %m是一年中的月份。 You want %M , as in t.strftime("%A %b %e @ %l:%M %p") . 您需要%M ,如t.strftime("%A %b %e @ %l:%M %p")

The end_of_day issue makes sense then, because end of day is 11:59. 那么end_of_day问题就有意义了,因为一天的结束时间是11:59。

If we're talking PHP date formatting syntax here, %I refers to a two-digit representation of the hour in 12-hour format, ie, 01 through 12. If you want 24-hour time, use %H instead. 如果在这里使用PHP日期格式语法, %I表示小时的两位数表示形式,即12小时制,即01到12。如果要使用24小时制,请改用%H

And %m is months, while %M is minutes. %m是几个月,而%M是分钟。

See the PHP documentation here . 此处查看PHP文档。

Edit: 编辑:

I am foolish, and thought you were talking about PHP because of the name of the function. 我很愚蠢,以为您在谈论PHP是因为该函数的名称。 Fortunately, the syntax for this in Ruby appears to be substantially the same, as you can see from j.'s answer. 幸运的是,从j。的答案中可以看出,Ruby中的语法基本相同。

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 . 您可以在此处找到有关time格式的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM