rails 时间转换

本文详细介绍了如何使用 strftime 方法来格式化时间。通过不同的指令符,如 %a、%A、%b、%B 等,可以实现日期和时间的各种显示格式。例如,可以将时间显示为星期的缩写形式或全称,月份也可以用缩写或全称展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. ------------------------------------------------------- Time#strftime
2. time.strftime( string ) => string
3. ---------------------------------------------------------------------
4. Formats _time_ according to the directives in the given format
5. string. Any text not listed as a directive will be passed through
6. to the output string.
7.
8. Format meaning:
9.
10. %a - The abbreviated weekday name ("Sun")
11. %A - The full weekday name ("Sunday")
12. %b - The abbreviated month name ("Jan")
13. %B - The full month name ("January")
14. %c - The preferred local data and time representation
15. %d - Day of the month (01..31)
16. %H - Hour of the day, 24-hour clock (00..23)
17. %I - Hour of the day, 12-hour clock (01..12)
18. %j - Day of the year (001..366)
19. %m - Month of the year (01..12)
20. %M - Minute of the hour (00..59)
21. %p - Meridian indicator ("AM" or "PM")
22. %S - Second of the minute (00..60)
23. %U - Week number of the current year, starting with the first Sunday as the first
24. day of the first week (00..53)
25. %W - Week number of the current year, starting with the first Monday as the first
26. day of the first week (00..53)
27. %w - Day of the week (Sunday is 0, 0..6)
28. %x - Preferred representation for the date alone, no time
29. %X - Preferred representation for the time alone, no date
30. %y - Year without a century (00..99)
31. %Y - Year with century
32. %Z - Time zone name
33. %% - Literal "%" character
34.
35. t = Time.now
36. t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
37. t.strftime("at %I:%M%p) #=> "at 08:56AM"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值