gpt4 book ai didi

ruby-on-rails - 如何在 Rails 3 中用 'hours:minutes:seconds' 显示时间?

转载 作者:行者123 更新时间:2023-12-04 03:36:33 25 4
gpt4 key购买 nike

我花了很多时间来回答我的问题,但我什么也没找到。

如何以“小时:分钟:秒”显示时间格式,例如:20:30:12?我试图将区域设置从 >time: "%H:%M"< 更改为 >time: "%H:%M:%S"< 因为我看到 %S 是秒,但没有效果:(

我做错了什么?

感谢您的回答!

最佳答案

使用 Time#strftime 方法

在你的情况下:

t = Time.now
t.strftime("%H:%M:%S") #=> "12:09:34"

不同的格式:
 %a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character

然后你可以创建这样的格式:
t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"

关于ruby-on-rails - 如何在 Rails 3 中用 'hours:minutes:seconds' 显示时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com