微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

js格式化时间的简单实例

<div class="jb51code">
<pre class="brush:js;">
Date.prototype.format = function(format) { //author: meizz
let o = {
"M+": this.getMonth() + 1,//月份
"d+": this.getDate(),//日
"H+": this.getHours(),//小时
"m+": this.getMinutes(),//分
"s+": this.getSeconds(),//秒
"q+": Math.floor((this.getMonth() + 3) / 3),//季度
"f+": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(format))
format = format.replace(RegExp.$1,(this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let 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 d=new Date();
d.format('yyyy/MM/dd HH:mm');
//"2016/11/25 10:01"

以上就是本文的全部内容,希望对大家有所帮助,同时也希望多多支持编程之家!

原文地址:https://www.jb51.cc/js/44132.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐