flickr api将发布日期作为unix时间戳之一提供:“ The posted date is always passed around as a unix timestamp, which is an unsigned integer specifying the number of seconds since Jan 1st 1970 GMT.
”
例如,这是日期“ 1100897479
”。如何使用Ruby on Rails格式化?
flickr api将发布日期作为unix时间戳之一提供:“ The posted date is always passed around as a unix timestamp, which is an unsigned integer specifying the number of seconds since Jan 1st 1970 GMT.
”
例如,这是日期“ 1100897479
”。如何使用Ruby on Rails格式化?
Answers:
一旦解析了时间戳字符串并有了一个时间对象(有关详细信息,请参阅其他答案),就可以使用Rails中的Time.to_formatted_s。它具有多种内置格式,您可以使用符号指定。
引用:
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:time) # => "06:10"
time.to_s(:time) # => "06:10"
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:number) # => "20070118061017"
time.to_formatted_s(:short) # => "18 Jan 06:10"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
(Time.to_s是别名)
您还可以定义自己的格式-通常在初始化程序中(感谢Dave Newton指出这一点)。这是这样做的:
# config/initializers/time_formats.rb
Time::DATE_FORMATS[:month_and_year] = "%B %Y"
Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
这是我的答案
因此,首先您需要将时间戳转换为实际的Ruby日期/时间。如果您只是从Facebook以字符串或int形式收到它,则需要执行以下操作:
my_date = Time.at(timestamp_from_facebook.to_i)
好,现在假设您已经有日期对象...
to_formatted_s是一个方便的Ruby函数,它将日期转换为格式化的字符串。
以下是其用法的一些示例:
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:time) # => "06:10"
time.to_s(:time) # => "06:10"
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:number) # => "20070118061017"
time.to_formatted_s(:short) # => "18 Jan 06:10"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
如您所见::db,:number,:short ...是自定义日期格式。
要添加自己的自定义格式,您可以创建以下文件:config / initializers / time_formats.rb并在其中添加自己的格式,例如以下示例:
Date::DATE_FORMATS[:month_day_comma_year] = "%B %e, %Y" # January 28, 2015
其中:month_day_comma_year是格式的名称(您可以将其更改为所需的任何名称),%B%e,%Y是UNIX日期格式。
这是有关Unix日期语法的快速备忘单,因此您可以快速设置自定义格式:
From http://linux.die.net/man/3/strftime
%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)
%e - Day of the month without leading 0 (1..31)
%g - Year in YY (00-99)
%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"
希望这对您有所帮助。如果有人喜欢的话,我也做了这个指南的github要点。
@question.created_at.to_formatted_s(:month_day_comma_year)
但我得到了#2019-07-27 03:27:41 UTC
DateTime
可以把它也用这个?
如果是在视图侧使用,则最好将其包装在帮助器中。
config/initializers/time_formats.rb
对于Rails来说更惯用了。
@CMW的答案是钱。我已添加此答案作为如何配置初始化程序的示例,以便Date和Time对象都能获得格式
config / initializers / time_formats.rb
date_formats = {
concise: '%d-%b-%Y' # 13-Jan-2014
}
Time::DATE_FORMATS.merge! date_formats
Date::DATE_FORMATS.merge! date_formats
另外,以下两个命令将遍历DATE_FORMATS
当前环境中的所有内容,并以每种格式显示今天的日期和时间:
Date::DATE_FORMATS.keys.each{|k| puts [k,Date.today.to_s(k)].join(':- ')}
Time::DATE_FORMATS.keys.each{|k| puts [k,Time.now.to_s(k)].join(':- ')}
首先,您需要将时间戳转换为实际的Ruby日期/时间。如果您只是从Facebook以字符串或int形式收到它,则需要执行以下操作:
my_date = Time.at(timestamp_from_facebook.to_i)
然后,要在视图中很好地设置格式,您可以使用to_s
(用于默认格式):
<%= my_date.to_s %>
请注意,如果您不放置to_s
,则如果您在视图或字符串中使用它,则默认情况下仍会调用它,例如,以下to_s
日期也会在日期上调用:
<%= "Here is a date: #{my_date}" %>
或者,如果您希望以特定方式格式化日期(例如,使用“ d / m / Y”),则可以strftime
按照其他答案中的说明使用。
puts Time.at('1100897479')
-无法将String转换为精确数字(TypeError)
由于时间戳记是自UNIX时代以来的秒数,因此可以将DateTime.strptime(“字符串解析时间”)与正确的说明符一起使用:
Date.strptime('1100897479', '%s')
#=> #<Date: 2004-11-19 ((2453329j,0s,0n),+0s,2299161j)>
Date.strptime('1100897479', '%s').to_s
#=> "2004-11-19"
DateTime.strptime('1100897479', '%s')
#=> #<DateTime: 2004-11-19T20:51:19+00:00 ((2453329j,75079s,0n),+0s,2299161j)>
DateTime.strptime('1100897479', '%s').to_s
#=> "2004-11-19T20:51:19+00:00"
请注意,您必须require 'date'
使它起作用,然后您可以将其称为Date.strptime
(如果只关心日期)或DateTime.strptime
(如果想要日期和时间)。如果需要其他格式,则可以在其上调用DateTime#strftime(如果格式字符串比较麻烦,请查看strftime.net),也可以使用诸如的内置方法之一rfc822
。