日期:2014-05-16  浏览次数:20398 次

javascript将long格式化为日期显示

这段时间做项目,发现日期类型在格式化jsonp格式数据后,变成了long的一窜数字,没有办法本人采取了措施。

1、date类型转换为String,json格式化后正常,单会多出来一个属性。

?

2、使用js直接格式化long为date显示,代码如下:

?

<script language="javascript">????????
??? Date.prototype.format = function (format) {??
??????? var o = {??
??????????? "M+": this.getMonth() + 1,??
??????????? "d+": this.getDate(),??
??????????? "h+": this.getHours(),??
??????????? "m+": this.getMinutes(),??
??????????? "s+": this.getSeconds(),??
??????????? "q+": Math.floor((this.getMonth() + 3) / 3),??
??????????? "S": this.getMilliseconds()??
??????? }??
??????? if (/(y+)/.test(format)) {??
??????????? format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));??
??????? }??
??????? for (var k in o) {??
??????????? if (new RegExp("(" + k + ")").test(format)) {??
??????????????? format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));??
??????????? }??
??????? }??
??????? return format;??
??? }????
??? function getFormatDateByLong(l, pattern) {??
??????? return getFormatDate(new Date(l), pattern);??
??? }?????
??? function getFormatDate(date, pattern) {??
??????? if (date == undefined) {??
??????????? date = new Date();??
??????? }??
??????? if (pattern == undefined) {??
??????????? pattern = "yyyy-MM-dd hh:mm:ss";??
??????? }??
??????? return date.format(pattern);??
??? }???????
??? alert(getFormatDateByLong(1279829423000, "yyyy-MM-dd"));??
??????
</script>?

本人最终采取了第二种。