Convert yyyy-mm-dd to UTC in Javascript

本秂侑毒 提交于 2021-02-05 02:36:45

问题


I need to convert a date in yyyy-mm-dd like 2011-12-30 to UTC using only javascript. How?


回答1:


var utc = new Date('2011-12-30').toUTCString();

jsFiddle.




回答2:


If you're having problems getting the other listed solution to work in firefox or safari you can use: http://www.datejs.com/

myDate = new Date.parse("2011-12-30")
myUTCDate = Date.UTC(myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), myDate.getHours(), myDate.getMinutes(), myDate.getSeconds(), myDate.getMilliseconds());

Voila!!




回答3:


This is very simple method to convert String to Date in JavaScript

var msomtDate = Date.parse('Here Your Date String'+' UTC',"yyyy/MM/dd HH:mm:ss");



回答4:


var toUTC = function (date) {
    var newDate = new Date();
    newDate.setTime(date.getTime() + (date.getTimezoneOffset() * 60 * 1000));
    return newDate;
};

console.log(toUTC(new Date('2011-12-30')));


来源:https://stackoverflow.com/questions/6273318/convert-yyyy-mm-dd-to-utc-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!