任何人都可以在此功能中发现任何错误吗?.. 这是一个函数,它接收类型为 yyyy-mm-dd hh:mm:ss aa 的字符串并转换为 UTC 并构建字符串 yyyy-mm-dd hh:mm:ss。
function LocalTimetoUTC(localTime)
{
var time = localTime.split(" "); //Received :- yyyy-mm-dd hh:mm:ss aa
var yearday = time[0].split("-");
var dateTime = time[1].split(":");
var ampm = time[2];
var hours = 0;
var year = yearday[0];
var month = yearday[1]-1;
var day = yearday[2];
hours = dateTime[0];
var minutes = dateTime[1];
var seconds = dateTime[2];
/* We have to first convert it to 24 hour format
* 12:00:00 AM : 00:00:00
* 12:00:00 PM : 12:00:00
* Anytime other than 12
* 1:00:00 AM : 1:00:00
* 1:00:00 PM : 13:00:00
*/
if(ampm == "PM")
{
//If it is 12PM, adding 12 will create a problem
if(hours != 12)
{
hours +=12;
}
}
else //AM CASE
{
if(hours == 12)
{
hours = 00;
}
}
var now = new Date(year,month,day,hours,minutes,seconds);
var utcString = now.getUTCFullYear()+"-"
+(now.getUTCMonth()+1)+"-"+now.getUTCDate()+""
+now.getUTCHours()+":"+now.getUTCMinutes()+":"+now.getUTCSeconds();
return utcString;
}