javascript 時間格式輸出FormatDate函數
javascript沒有提供像fmt標簽一樣對日期時間型內容格式輸入的函數:
下面是我下的時間輸出函數,使用時直接放到標簽內,調用。代碼如下
Date.prototype.Format = function(fmt) { //author: meizz
if (this == "Invalid Date") {
return "";
}
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(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
for ( var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
: (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
} 使用時直接用
new Date( 時間變量 ).Format("yyyy-MM-dd HH:mm:ss")
本文由用戶 bbw3 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!