How do you convert a JavaScript date to UTC?

后端 未结 29 2046
无人共我
无人共我 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:12

    Convert to ISO without changing date/time

    var now = new Date(); // Fri Feb 20 2015 19:29:31 GMT+0530 (India Standard Time) 
    var isoDate = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString();
    //OUTPUT : 2015-02-20T19:29:31.238Z
    

    Convert to ISO with change in date/time(date/time will be changed)

    isoDate = new Date(now).toISOString();
    //OUTPUT : 2015-02-20T13:59:31.238Z 
    

    Fiddle link

提交回复
热议问题