如何转换本地时间为UTC? [英] How do I convert local time to UTC?
问题描述
我想我的本地时间(如下图所示)转换为UTC。当地时间可以属于用户所属的任何时区。我只是想将其转换为UTC格式。
当地时间:2015年12月4日19时05分48秒
块引用>如何在JavaScript或AngularJS达到这一要求?
更新
我原来的日期/时间字符串为这样的星期一2016年3月28日23:59:59 GMT -0600(中部夏令时间)的,我让他们转换成的2016- 03-28 23:59:59 CDT的的要求日期格式。但同样,我需要这个临时的输出转换为UTC在同一个临时的输出格式的YYYY-MM-DD HH:MM:SS的。我如何做到这一点。
解决方案您可以在原有基础上(本地)日期的UTC值,创建一个新的本地日期。
\r\r\r\rVAR = LOCALDATE parseDate('2015年12月4日19时05分48秒CDT '); //带或不带CDT(仍然有效)\r
\r
document.body.innerHTML = [\r
- 本地时间:'+ formatDate(LOCALDATE)\r
' - UTC时间:'+ formatDate(localDateToUTC(LOCALDATE))\r
]。加入('\\ n');\r
\r
功能parseDate(dateString){\r
变种令牌= dateString.split(/ [ - :] / g)的.slice(0,6).MAP(函数(令牌){返回parseInt函数(令牌,10);});\r
令牌[1] - = 1; //从一个月减1。\r
返回的newInstance(日期,标记);\r
}\r
\r
功能formatDate(日期){\r
功能垫(VAL,图案){回报(+模式VAL).substr(-pattern.length); }\r
返回date.getFullYear()+' - '+垫(date.getMonth()+ 1,'00')+' - '+垫(date.getDate(),'00')+''+\r
垫(date.getHours(),'00')+':'+垫(date.getMinutes(),'00')+':'+垫(date.getSeconds(),'00');\r
}\r
\r
功能localDateToUTC(LOCALDATE){\r
返回新的日期(localDate.getUTCFullYear(),localDate.getUTCMonth(),localDate.getUTCDate()\r
localDate.getUTCHours(),localDate.getUTCMinutes(),localDate.getUTCSeconds());\r
}\r
\r
功能的newInstance(clazz所争论,范围){\r
返回新(Function.prototype.bind.apply(clazz所[适用范围] .concat(参数)));\r
}\r
{体\r
FONT-FAMILY:等宽;\r
空格:pre;\r
}\r
I want to convert my local time (shown below) to UTC. Local time can belong to any timezone which a user belongs to. I just want to convert it to UTC format.
Local Time : '2015-12-04 19:05:48'
How do I achieve this requirement in JavaScript or AngularJS?
Update
My Original date/time string as like this "Mon March 28 2016 23:59:59 GMT -0600 (CENTRAL DAYLIGHT TIME)" and I have converted them to "2016-03-28 23:59:59 CDT" as requested date format. But again, I need to convert this interim output in to UTC in the same interim output format "YYYY-MM-DD HH:MM:SS". How do I achieve this
解决方案You can create a new local date based on the UTC values of the original (local) date.
var localDate = parseDate('2015-12-04 19:05:48 CDT'); // With or without CDT (still works) document.body.innerHTML = [ '- Local Time: ' + formatDate(localDate), '- UTC Time: ' + formatDate(localDateToUTC(localDate)) ].join('\n'); function parseDate(dateString) { var tokens = dateString.split(/[-: ]/g).slice(0, 6).map(function(token) { return parseInt(token, 10); }); tokens[1] -= 1; // Subtract 1 from month. return newInstance(Date, tokens); } function formatDate(date) { function pad(val, pattern) { return (pattern + val).substr(-pattern.length); } return date.getFullYear() + '-' + pad(date.getMonth() + 1, '00') + '-' + pad(date.getDate(), '00') + ' ' + pad(date.getHours(), '00') + ':' + pad(date.getMinutes(), '00') + ':' + pad(date.getSeconds(), '00'); } function localDateToUTC(localDate) { return new Date(localDate.getUTCFullYear(), localDate.getUTCMonth(), localDate.getUTCDate(), localDate.getUTCHours(), localDate.getUTCMinutes(), localDate.getUTCSeconds()); } function newInstance(clazz, arguments, scope) { return new (Function.prototype.bind.apply(clazz, [scope].concat(arguments))); }
body { font-family: monospace; white-space: pre; }
这篇关于如何转换本地时间为UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!