How do you convert a JavaScript date to UTC?

后端 未结 29 2050
无人共我
无人共我 2020-11-22 00:50

Suppose a user of your website enters a date range.

2009-1-1 to 2009-1-3

You need to send this date to a server for some processing, but th

29条回答
  •  长情又很酷
    2020-11-22 01:11

    Using moment package, you can easily convert a date string of UTC to a new Date object:

    const moment = require('moment');
    let b = new Date(moment.utc('2014-02-20 00:00:00.000000'));
    let utc = b.toUTCString();
    b.getTime();
    

    This specially helps when your server do not support timezone and you want to store UTC date always in server and get it back as a new Date object. Above code worked for my requirement of similar issue that this thread is for. Sharing here so that it can help others. I do not see exactly above solution in any answer. Thanks.

提交回复
热议问题