`
liyonghui160com
  • 浏览: 761651 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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> 

本人最终采取了第二种。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics