[英]Convert yyyy-mm-dd to UTC in Javascript
I need to convert a date in yyyy-mm-dd like 2011-12-30 to UTC using only javascript.我需要仅使用 javascript 将 yyyy-mm-dd 中的日期(如 2011-12-30)转换为 UTC。 How?如何?
If you're having problems getting the other listed solution to work in firefox or safari you can use: http://www.datejs.com/如果您在让其他列出的解决方案在 firefox 或 safari 中工作时遇到问题,您可以使用: 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!!瞧!!
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')));
This is very simple method to convert String to Date in JavaScript这是在 JavaScript 中将字符串转换为日期的非常简单的方法
var msomtDate = Date.parse('Here Your Date String'+' UTC',"yyyy/MM/dd HH:mm:ss");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.