[JavaScript]日期格式化工具(Date format)
程序员文章站
2022-07-14 23:51:38
...
DateUtil.js
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 now = new Date();
var today = now.format('yyyy-MM-dd');// yyyy-MM-dd 可自定义
var nowTime = now.format('hh:mm');// hh:mm 可自定义
console.log(today+' '+nowTime);
下一篇: 统计某种性质的几位数
推荐阅读
-
javascript 格式化时间日期函数代码脚本之家修正版
-
javascript实现的距离现在多长时间后的一个格式化的日期
-
JavaScript日期工具类DateUtils定义与用法示例
-
mysql From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT日期函数
-
PowerShell中使用Get-Date获取日期时间并格式化输出的例子
-
php Smarty date_format [格式化时间日期]
-
JavaScript 中Date对象的格式化代码方法汇总
-
Java开发笔记(三十九)日期工具Date
-
Format javascript Date to yyyy-mm-dd
-
javascript format Date 简单的时间格式化