我有以下代码
INPUT:
var dateString = "10/30/2014 02:15 PM +00:00";
var a = dateString .split(/[^0-9]/);
var dateVal=new Date(a[0],a[1]-1,a[2],a[3],a[4],a[5]);
var minutes=dateVal.setMinutes(dateVal.getMinutes() + 330);
var hours = dateVal.getHours();
if(parseInt(hours,10) < 10){
hours = "0"+hours;
}
var minutes = parseInt(dateVal.getMinutes());
var ampm = parseInt(hours, 10) >= 12 ? 'PM' : 'AM';
hours = parseInt(hours % 12);
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
strTime = hours + ':' + minutes + ' ' + ampm;
return strTime
输出:
Actual: 7:45 AM
Expected : 7:45 PM
我能够获得时区转换但无法获得所需的输出,因为我喜欢得到7:45 PM,但我总是采用相反的方式。 有些人可以帮我解决这个问题。
答案 0 :(得分:1)
问题是,当你转换字符串时,中午的时间没有被检测到。尝试搜索PM,如果找到则将你的小时数增加12:
var dateString = "10/30/2014 02:15 PM +00:00";
var a = dateString .split(/[^0-9]/);
if(dateString.indexOf("PM") != -1) a[3] = Number(a[3]) + 12;
var dateVal=new Date(a[0],a[1]-1,a[2],a[3],a[4],a[5]);