最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment js utc conversions not working as expected, behind date object value - Stack Overflow

programmeradmin8浏览0评论

I am trying to convert a particular date to utc(save it in db) and after fetching it show it as local time. When I use moment there is always a half an hour delay as pared to js Date object,any idea why?

Date is 8th May 2016, browser timezone is India

Convert Date to Utc: Moment:

moment('2016-05-08', 'YYYY-MM-DD').utc().format('YYYY-MM-DD HH:MM:SS Z')
Result: "2016-05-07 18:05:00 +00:00"

Date:

new Date('2016/05/08').toUTCString()
Result: "Sat, 07 May 2016 18:30:00 GMT"

I believe 18:30 is the right answer and not 18:05

From Utc to date: Moment:

moment('2016-05-07 18:05:00 +00:00', 'YYYY-MM-DD HH:MM:SS Z').format('YYYY-MM-DDTHH:MM:SS')
Result: "2016-05-07T23:05:00" //This should be 8th May since I had started with 8th May

Date:

new Date("Sat, 07 May 2016 18:30:00 GMT").toString()
Result: "Sun May 08 2016 00:00:00 GMT+0530 (India Standard Time)" //this is the correct answer since I had initially started with 8Th May.

Any reason why moment has this lag?

I am trying to convert a particular date to utc(save it in db) and after fetching it show it as local time. When I use moment there is always a half an hour delay as pared to js Date object,any idea why?

Date is 8th May 2016, browser timezone is India

Convert Date to Utc: Moment:

moment('2016-05-08', 'YYYY-MM-DD').utc().format('YYYY-MM-DD HH:MM:SS Z')
Result: "2016-05-07 18:05:00 +00:00"

Date:

new Date('2016/05/08').toUTCString()
Result: "Sat, 07 May 2016 18:30:00 GMT"

I believe 18:30 is the right answer and not 18:05

From Utc to date: Moment:

moment('2016-05-07 18:05:00 +00:00', 'YYYY-MM-DD HH:MM:SS Z').format('YYYY-MM-DDTHH:MM:SS')
Result: "2016-05-07T23:05:00" //This should be 8th May since I had started with 8th May

Date:

new Date("Sat, 07 May 2016 18:30:00 GMT").toString()
Result: "Sun May 08 2016 00:00:00 GMT+0530 (India Standard Time)" //this is the correct answer since I had initially started with 8Th May.

Any reason why moment has this lag?

Share Improve this question asked May 6, 2016 at 17:55 wallopwallop 2,5851 gold badge28 silver badges39 bronze badges 1
  • It is time-zone related. Either the puter's time is off or moment doen't have India's 1/2 hourzone -- or some other, related, mismatch. en.wikipedia/wiki/Indian_Standard_Time – Jeremy J Starcher Commented May 6, 2016 at 18:22
Add a ment  | 

1 Answer 1

Reset to default 6

You are using capital M instead of mm for minutes, and it's giving you months. Switch to little m and everything is fine.

moment('2016-05-08', 'YYYY-MM-DD').utc().format('YYYY-MM-DD HH:mm:ss Z')
"2016-05-07 18:30:00 +00:00"

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论