あいつの日誌β

働きながら旅しています。

また DateTime でポカしたので備忘録

DB に ymd 形式(utc)で保持している文字列を JST に変換するときにポカした

moment = require 'moment'

ymd = '2014-12-18 00:00:00' # utc

dt = new Date(ymd)
m = moment.utc new Date(ymd)

console.log dt          # Thu Dec 18 2014 00:00:00 GMT+0900 (JST)
console.log m.format()  # 2014-12-17T15:00:00+00:00

これが本来やりたかった事

moment = require 'moment'

ymd = '2014-12-18 00:00:00' # utc

dt = new Date(ymd)
m = moment dt 

console.log dt          # Thu Dec 18 2014 00:00:00 GMT+0900 (JST)
console.log m.format()  # 2014-12-18T00:00:00+09:00

でもこうやればもっと見通しがよいので気をつける

moment = require 'moment'

m = moment.utc('2014-12-18 00:00:00').local()
console.log m.format()  # 2014-12-18T00:00:00+09:00