当我在Meetup.com上遇到一个聚会小组寻找即将发生的事件时,我最终得到了这个JSON片段,我相信这是在下次聚会安排的时候。
"utc_offset": -21600000,
"time": 1415752200000,
"waitlist_count": 0,
"updated": 1382991416000,
我正在尝试将其转换为Ruby中的人性化日期/时间,但我不确定如何做到这一点?
答案 0 :(得分:0)
这里的时间戳似乎是以毫秒为单位。将它们除以1000,ruby可以将秒数转换为时间。
您可以在Ruby中使用Time.at(i)
将整数转换为时间。
Time.at(1415752200000/1000) # => 2014-11-12 08:30:00 +0800
Time.at(1382991416000/1000) # => 2013-10-29 04:16:56 +0800
有关详细信息,请参阅http://www.ruby-doc.org/core-2.1.3/Time.html#method-c-at
答案 1 :(得分:0)
正如我从你的time
对象中理解的那样,它的返回时间(以毫秒为单位)不是日期。
为此,您可以使用 strftime 方法和Time
类
seconds = 1415752200000/1000
Time.at(seconds).strftime("%H:%M:%S")
=> "06:00:00"
根据您的要求,您可以修改strftime()
方法的格式,例如
Time.at(seconds).strftime("%Y-%B-%d %H:%M:%S")
=> "2014-November-12 06:00:00"
可用的格式选项:
%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)
%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 firstday 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