Ruby 时间规范化函数strftime使用大全

通过Time.new获取当前时间:

t = Time.now
=> 2013-09-12 22:49:27 +0530

使用strftime获得具体的年份,月份或日期等等,常用:

CODE OUTPUT DESCRIPTION
t.strftime("%H") => "22" # Gives Hour of the time in 24 hour clock format
t.strftime("%I") => "10" # Gives Hour of the time in 12 hour clock format
t.strftime("%M") => "49" # Gives Minutes of the time
t.strftime("%S") => "27" # Gives Seconds of the time
t.strftime("%Y") => "2013" # Gives Year of the time
t.strftime("%m") => "09" # Gives month of the time
t.strftime("%d") => "12" # Gives day of month of the time
t.strftime("%w") => "4" # Gives day of week of the time
t.strftime("%a") => "Thu" # Gives name of week day in short form of the
t.strftime("%A") => "Thursday" # Gives week day in full form of the time
t.strftime("%b") => "Sep" # Gives month in short form of the time
t.strftime("%B") => "September" # Gives month in full form of the time
t.strftime("%y") => "13" # Gives year without century of the time
t.strftime("%Y") => "2013" # Gives year without century  of the time
t.strftime("%Z") => "IST" # Gives Time Zone of the time
t.strftime("%p") => "PM" # Gives AM / PM of the time
混合使用:

t.strftime("%H:%M:%S")
 => "22:49:27"

所有方法:

For 'a' to 'z'
CODE OUTPUT
t.strftime('%a') => Thu
t.strftime('%b') => Jan
t.strftime('%c') => Thu Jan 23 16:38:02 2014
t.strftime('%d') => 23
t.strftime('%e') => 23
t.strftime('%f') => %f # Not Useful
t.strftime('%g') => 14
t.strftime('%h') => Jan
t.strftime('%i') => %i # Not Useful
t.strftime('%j') => 023
t.strftime('%k') => 16
t.strftime('%l') => 4
t.strftime('%m') => 01
t.strftime('%n') => # Not Useful
t.strftime('%o') => %o
t.strftime('%p') => PM
t.strftime('%q') => %q
t.strftime('%r') => 04:38:02 PM
t.strftime('%s') => 1390475282
t.strftime('%t') => # Not Useful
t.strftime('%u') => 4
t.strftime('%v') => 23-JAN-2014
t.strftime('%w') => 4
t.strftime('%x') => 01/23/14
t.strftime('%y') => 14
t.strftime('%z') => +0530
For 'A' to 'Z'
CODE OUTPUT
t.strftime('%A') => Thursday
t.strftime('%B') => January
t.strftime('%C') => 20
t.strftime('%D') => 01/23/14
t.strftime('%E') => %E # Not Useful
t.strftime('%F') => 2014-01-23
t.strftime('%G') => 2014
t.strftime('%H') => 16
t.strftime('%I') => 04
t.strftime('%J') => %J # Not Useful
t.strftime('%K') => %K # Not Useful
t.strftime('%L') => 485
t.strftime('%M') => 38
t.strftime('%N') => 485141000
t.strftime('%O') => %O # Not Useful
t.strftime('%P') => pm
t.strftime('%Q') => %Q # Not Useful
t.strftime('%R') => 16:38
t.strftime('%S') => 02
t.strftime('%T') => 16:38:02
t.strftime('%U') => 03
t.strftime('%V') => 04
t.strftime('%W') => 03
t.strftime('%X') => 16:38:02
t.strftime('%Y') => 2014
t.strftime('%Z') => IST

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值