简单的时间处理

function dateOperation(date = new Date()) {
this.myDate = new Date(date);
// 格式化时间
this.getFormat = (format = 'YYYY-MM-DD HH:mm:ss', date = this.myDate) => {
date = new Date(date);
const config = {
YYYY: date.getFullYear(),
MM: this.repair(date.getMonth() + 1),
DD: this.repair(date.getDate()),
HH: this.repair(date.getHours()),
mm: this.repair(date.getMinutes()),
ss: this.repair(date.getSeconds()),
}
for (const configKey in config) {
format = format.replace(configKey, config[configKey]);
}
return format;
}
// 补零
this.repair = (month) => {
return month < 10 ? '0' + month : month;
}
// 获取向前向后多少天的时间 以天数为单位
this.getBeforeDate = (n, date = this.myDate) => {
date = new Date(date);
let calculation = 1000 * 60 * 60 * 24 * n;
let getTimeStr = date.getTime();
this.myDate = new Date(getTimeStr + calculation)
return this;
}
}

let date1 = new dateOperation();
console.log(date1.getFormat());
console.log(date1.getBeforeDate(5).getFormat())
console.log(date1.getFormat());
posted @   莣ㄋ噯  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示