2014-09-22 47 views
0

當我在Meetup.com上找到一個Meetup組來尋找即將發生的事件時,我最終得到了這段JSON,我相信這是下次聚會的時間安排。Meetup.com API事件迄今爲止的時間?

"utc_offset": -21600000, 
"time": 1415752200000, 
"waitlist_count": 0, 
"updated": 1382991416000, 

我想將它轉換爲Ruby中的人性化日期/時間,但我不知道如何做到這一點?

回答

0

看來這裏的時間標記以毫秒爲單位。將它們除以1000,紅寶石可以將秒數轉換爲時間。

您可以在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

0

當我從你的time反對它不是隻返回時間(以毫秒爲單位)日期的理解。

對於您可以試試這個使用strftimeTime方法類

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