JavaScript时间日期格式化
生活随笔
收集整理的這篇文章主要介紹了
JavaScript时间日期格式化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/** 時間對象的格式化;*/
Date.prototype.format = function(format) {/** eg:format="YYYY-MM-dd hh:mm:ss";*/var o = {"M+" :this.getMonth() + 1, // month"d+" :this.getDate(), // day"h+" :this.getHours(), // hour"m+" :this.getMinutes(), // minute"s+" :this.getSeconds(), // second"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter"S" :this.getMilliseconds() // millisecond}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;
}
測試:
var nowDate = new Date().format("yyyy-MM-dd");
轉載于:https://www.cnblogs.com/hechaoyang/archive/2011/03/24/1993780.html
總結
以上是生活随笔為你收集整理的JavaScript时间日期格式化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一炮多少钱啊?
- 下一篇: 衣柜门大概多少钱一平方