How do you convert a JavaScript date to UTC?

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

    So this is the way I had to do it because i still wanted a JavaScript date object to manipulate as a date and unfortunantly alot of these answers require you to go to a string.

    //First i had a string called stringDateVar that i needed to convert to Date
    var newDate = new Date(stringDateVar)
    
    //output: 2019-01-07T04:00:00.000Z
    //I needed it 2019-01-07T00:00:00.000Z because i had other logic that was dependent on that 
    
    var correctDate = new Date(newDate.setUTCHours(0))
    
    //This will output 2019-01-07T00:00:00.000Z on everything which allows scalability 
    

提交回复
热议问题