2012-10-31 239 views
2

抵消我有一個DateTime UTC中,想添加一個偏移量以秒添加日期時間(秒)

#json parsed - utc offset in seconds - turned into integer 
@utc_offset = result["UTCOffsetMillis"].to_i 
#json parsed - utc date 
start_date_string = result["startDate"].split("-") 
start_date = DateTime.new(start_date_string[0].to_i, start_date_string[1].to_i,start_date_string[2].to_i) 

如何添加這個以秒偏移START_DATE提供?

的Rails 3.2.3 紅寶石1.9.2p320

回答

6

要在以秒偏移添加到DateTime你可以使用:

DateTime.new(2012, 10, 31) + 5.seconds 
#=> Wed, 31 Oct 2012 00:00:05 +0000 

要改變時區,我會使用Time代替的DateTime

Time.new(2012, 10, 31, 0, 0, 0, 3600).localtime 
#=> 2014-10-31 00:00:00 +0100 

3600是以秒爲單位的UTC偏移量。