//日期模块
Date.prototype.format = function (fmt) {
let o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours() % 12 === 0 ? 12 : this.getHours() % 12, //小时
"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 (let 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;
}
let time1 = new Date().format("yyyy-MM-dd HH:mm:ss");
let time2 = new Date(time1).format("yyyy-MM-dd");
console.log(time1);
console.log(time2)
//封装一个倒计时函数
function countDown(news) {
let stopTime = +new Date(news) //用户输入的时间'2022-2-21 03:59:12'(注意实参格式)
let date = +new Date()
let time = (stopTime - date) / 1000
let d = parseInt.call(null, time / 60 / 60 / 24)
d = d < 10 ? '0' + d : d
let h = parseInt.call(null, time / 60 / 60 % 24)
h = h < 10 ? '0' + h : h
let m = parseInt.call(null, time / 60 % 60)
m = m < 10 ? '0' + m : m
let s = parseInt.call(null, time % 60)
s = s < 10 ? '0' + s : s
return d + '天' + h + '时' + m + '分' + s + '秒'
}
一些js小模块-日期模块
于 2022-02-15 23:33:30 首次发布