我正在向API发布日期,所需格式如下:
2014-12-01T01:29:18
我可以从模型中得到日期,如下所示:
Model.created_at.to_s
结果是:
2014-12-01 01:29:18 -0500
如何使用Rails或Ruby将其格式化为所需的格式,并删除-0500?
我正在向API发布日期,所需格式如下:
2014-12-01T01:29:18
我可以从模型中得到日期,如下所示:
Model.created_at.to_s
结果是:
2014-12-01 01:29:18 -0500
如何使用Rails或Ruby将其格式化为所需的格式,并删除-0500?
使用
Model.created_at.strftime("%FT%T")
哪里
%F - The ISO 8601 date format (%Y-%m-%d)
%T - 24-hour time (%H:%M:%S)
以下是一些常用的Date
和Time
格式列表,可以在strftime
方法中指定:
Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%C - year / 100 (round down. 20 in 2009)
%y - year % 100 (00..99)
%m - Month of the year, zero-padded (01..12)
%_m blank-padded ( 1..12)
%-m no-padded (1..12)
%B - The full month name (``January'')
%^B uppercased (``JANUARY'')
%b - The abbreviated month name (``Jan'')
%^b uppercased (``JAN'')
%h - Equivalent to %b
%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
%e - Day of the month, blank-padded ( 1..31)
%j - Day of the year (001..366)
Time (Hour, Minute, Second, Subsecond):
%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')
%M - Minute of the hour (00..59)
%S - Second of the minute (00..59)
%L - Millisecond of the second (000..999)
%N - Fractional seconds digits, default is 9 digits (nanosecond)
%3N millisecond (3 digits)
%6N microsecond (6 digits)
%9N nanosecond (9 digits)
%12N picosecond (12 digits)
有关strftime
方法的complete list种格式,请访问APIDock