js 將long型字符串轉換成日期格式

工做中不免會碰到日期的轉換,每每爲了方便,後臺都是把時間以long型(形如1343818800000)返回給web前端.再有前端本身根據頁面需求轉換成相應的日期格式.這裏將我經常使用的一個轉換時間的函數貼上:前端

//轉換時間函數
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;
} ;
//轉換long型爲日期字符串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);}function getSmpFormatDate(date, isFull) {    var pattern = "";    if (isFull == true || isFull == undefined) {        pattern = "yyyy-MM-dd hh:mm:ss";    } else if(isFull==2){        pattern = "yyyy-MM-dd hh:mm";    }    else {        pattern = "yyyy-MM-dd";    }    return getFormatDate(date, pattern);}function getSmpFormatDateByLong(date, isFull) {    if(date=='' || date==null){        return    }    return getSmpFormatDate(new Date(date), isFull);}
相關文章
相關標籤/搜索