简体   繁体   English

将日期字符串转换为utc时间格式javascript

[英]converting date string to utc time format javascript

i'm trying to convert this date string format "2014-12-04 12:00am" to a utc time with the format of "2014-11-29 01:00:12". 我正在尝试将此日期字符串格式“ 2014-12-04 12:00 am”转换为UTC时间,格式为“ 2014-11-29 01:00:12”。

i'm using this function 我正在使用此功能

 function formatDate(d){
          function addZero(n){
              return n < 10 ? '0' + n : '' + n;
          }

          return d.getUTCFullYear() +"-"+ addZero(d.getUTCMonth()+1) + "-" +addZero(d.getUTCDate()) + " " +
              addZero(d.getUTCHours()) + ":" + addZero(d.getUTCMinutes()) + ":" + addZero(d.getUTCMinutes());
      } 

for some reason when i pick 12am it's set the val to a 12pm and i'm not sure this function is working on all the possibilities 由于某种原因,当我选择凌晨12点时,将val设置为12pm,但我不确定此功能是否在所有可能的情况下都能正常工作

Try this JS : 试试这个JS:

var d = new Date("2014-12-04 12:00 am");
var n = d.toUTCString();
alert(n); // Output: Wed, 03 Dec 2014 18:30:00 GMT

You can see this link : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_toutcstring 您可以看到此链接: http : //www.w3schools.com/jsref/tryit.asp?filename=tryjsref_toutcstring

Try this one.. 试试这个

var utc =  moment.utc(new Date())
alert("UTC: " + utc.format('YYYY-MM-DD HH:mm:ss A'))//A for AM, PM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM