How do you convert a JavaScript date to UTC?

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

    I just discovered that the 1.2.3 version of Steven Levithan's date.format.js does just what I want. It allows you to supply a format string for a JavaScript date and will convert from local time to UTC. Here's the code I'm using now:

    // JavaScript dates don't like hyphens!    
    var rectifiedDateText = dateText.replace(/-/g, "/");
    var d = new Date(rectifiedDateText);
    
    // Using a predefined mask from date.format.js.
    var convertedDate = dateFormat(d, 'isoUtcDateTime'); 
    

提交回复
热议问题