问题
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