How do you convert a JavaScript date to UTC?

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

    var userdate = new Date("2009-1-1T8:00:00Z");
    var timezone = userdate.getTimezoneOffset();
    var serverdate = new Date(userdate.setMinutes(userdate.getMinutes()+parseInt(timezone)));
    

    This will give you the proper UTC Date and Time.
    It's because the getTimezoneOffset() will give you the timezone difference in minutes. I recommend you that not to use toISOString() because the output will be in the string Hence in future you will not able to manipulate the date

提交回复
热议问题