2015-12-31 48 views
0

I,M使用下面的Ruby版本和Rails 紅寶石:2.0.0p481 & 的Rails 4.1.1支持轉換的時區從其他時區IST在Ruby中

未能進行找到DateTimeclass中的任何方法將時間轉換爲IST。

在IRB中嘗試DateTime.in_time_zone,可以將IST轉換爲EST,但不能反過來。

2.0.0-p481 :003 > date = "Thu Jan 07 2016 16:20:00 GMT+0530 (India Standard Time)" 
    2.0.0-p481 :003 > date = date.in_time_zone('Eastern Time (US & Canada)') 
    2.0.0-p481 :003 > date 
     => Thu, 07 Jan 2016 05:50:00 EST -05:00 

想知道如何將其他齒區轉換爲IST。 請讓我知道,如果有什麼方法可以實現這一點。

+0

哪個時區的你想要的日期/時間轉換爲IST?你的情況是什麼? –

+0

是的,我想從任何時區轉換爲IST。從美國東部標準時間[Fri Dec 04 2015 11:35:00 GMT-0500(東部標準時間)]發送給IST。我無法找到任何紅寶石來實現這一目標。 – Bindu

+0

你從哪裏得到這些EST時間?他們是你解析的文本嗎? –

回答

1

ActiveSupport::TimeZone提供所有時區的名稱。您可以通過執行

ActiveSupport::TimeZone.all.map(&:name) 

對於剛剛US時區

ActiveSupport::TimeZone.us_zones.map(&:name) 

因此,與現有的時區更改時列出所有時區。像這樣

irb> date = DateTime.now 
=> Thu, 31 Dec 2015 22:15:59 +0530 

# convert to EST 
irb> date_est = date.in_time_zone("Eastern Time (US & Canada)") 
=> Thu, 31 Dec 2015 11:45:59 EST -05:00 

# convert to IST 
irb> date_ist = date_est.in_time_zone("Chennai") 
=> Thu, 31 Dec 2015 22:15:59 IST +05:30 

快樂編碼...

+0

謝謝,這有幫助! – Bindu