2011-10-07 47 views
0
Date.parseWeird = Date.prototype.parseWeird = function (d) { 
    return new Date(parseInt(d.replace(/[^\-\d]/g, ""), 10)); 
}; 

var newDate = Date.parseWeird("\/Date(1317847200000-0400)\/"); 

document.write(newDate); 

这将返回我提取时间Thu Oct 06 2011 02:10:00 GMT+0530 (India Standard Time)之日起输入反应

我想提取这个02:10:00,并转换到这个时候像这样9:30 P.M or A.M

+0

哪里日期从何而来?它看起来像来自服务器上.NET webmethod的默认日期,我是对的吗?你可以解决问题的服务器端然后(看看JSON序列化) –

+0

@Baszz:我没有访问服务器端...我必须在客户端.. –

回答

3

你就不能尝试这样的事情?

http://jsfiddle.net/wXvzt/

<h4>It is now 
<script type="text/javascript"> 
<!-- 
var currentTime = new Date() 
var hours = currentTime.getHours() 
var minutes = currentTime.getMinutes() 
if (minutes < 10){ 
minutes = "0" + minutes 
} 
document.write(hours + ":" + minutes + " ") 
if(hours > 11){ 
document.write("PM") 
} else { 
document.write("AM") 
} 
//--> 
</script> 
</h4> 
+0

我不想显示20:12,相反,我希望它是下午8点12分..任何想法如何我可以做你的代码.. –

+0

@JohnCooper它不显示20:12,它显示正确的东西,就像你想要的。 JMax刚刚发布了与我完全相同的内容。 – FreeSnow

+0

http://jsfiddle.net/wXvzt/看看这个小提琴,你会得到 –

1

要显示的时间与AM/PM,你可以使用这种脚本:

var hours = newDate.getHours() 
var minutes = newDate.getMinutes() 
if (minutes < 10){ 
    minutes = "0" + minutes 
} 
document.write(hours + ":" + minutes + " ") 
if(hours > 11){ 
    document.write("PM") 
} else { 
    document.write("AM") 
}