JavaScript日期格式化
2017-09-16 16:38
274 查看
采用扩展js Date的方式格式化日期,代码如下:
Date.prototype.Format = function (fmt) {
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;
}调用方式:var date = new Date().Format("yyyy-MM-dd");
Date.prototype.Format = function (fmt) {
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;
}调用方式:var date = new Date().Format("yyyy-MM-dd");
相关文章推荐
- 如何使用Javascript格式化日期显示
- javascript中根据表达式实现字符串格式化为日期——升级版
- javascript日期格式化
- JavaScript自定义日期格式化函数详细解析
- JavaScript日期格式化与正则表达式RegExp.$的用法
- JavaScript处理日期格式化
- JavaScript 日期Date格式化工具
- javascript日期格式化方法汇总
- javascript Date format(js日期格式化)
- javascript Date format(js日期格式化)
- JavaScript格式化日期的函数
- javascript Date format(js日期格式化)
- javascript扩展date对象格式化日期时间输出
- javascript Date format(js日期格式化)
- Javascript 日期格式化 相关操作
- JavaScript 日期格式化
- javascript Date format(js日期格式化)
- JS日期格式化之javascript Date format
- javascript Date format(js日期格式化)
- Javascript 解析,格式化日期 (转)