2012-08-13 14 views
2

我有2行代碼嘗試在JavaScript中放置開始和結束日期時間。 new Date('<%= DateTime.now.beginning_of_day.to_s %>')就是一個例子。 DateTime.now.beginning_of_day返回字符串2012-08-13T00:00:00-04:00,該字符串將JavaScript轉換爲2012/8/13 00:00。完善。但是,DateTime.yesterday.beginning_of_day.to_s返回的是2012年8月11日20:00的JavaScript翻譯的'2012-08-11 20:00:00:00:00'。我不想在前天晚上八點。我想要昨天午夜。我試過Date.yesterday.in_time_zone("Eastern Time (US & Canada)"),.utc和其他各種排列組合。我必須做些什麼來獲得過去與DateTime.now相同格式的日期?Rails格式DateTime.now單向和DateTime.yesterday另一個 - 我怎麼得到過去的日期格式如DateTime.now?

回答

2

試試這個:

DateTime.now.yesterday.beginning_of_day 
0

我會用Time.zone

那麼一些哈克TO_DATE回TO_TIME確保它在午夜zero'd出

new Date('<%= Time.zone.now.to_date.to_time.to_s %>') 

new Date('<%= (Time.zone.now - 1.day).to_date.to_time.to_s %>') 

你應該能夠使用Time.zone.now.yesterday.to_date.to_time.to_s而不是 - 1.day