How do you convert a JavaScript date to UTC?

后端 未结 29 2708
无人共我
无人共我 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 00:56

    You can use the following method to convert any js date to UTC:

    let date = new Date(YOUR_DATE).toISOString()
    
    // It would give the date in format "2020-06-16T12:30:00.000Z" where Part before T is date in YYYY-MM-DD format, part after T is time in format HH:MM:SS  and Z stands for UTC - Zero hour offset
    

提交回复
热议问题