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

来源:互联网 发布:渡边琢斗 知乎 编辑:程序博客网 时间:2024/06/05 03:57

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

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

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

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

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

所有方法:

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

0 0
原创粉丝点击