JS时间加减(亲自操作有效)

/************************时间函数 开始******************************************/
function computeDate(times, type, days, hours, minutes, seconds) {
// 全部需要减掉的时间戳
let all_time = new Date(times).getTime();
switch (type) {
case "add":
all_time +=
days * 24 * 60 * 60 * 1000 +
hours * 60 * 60 * 1000 +
minutes * 60 * 1000 +
seconds * 1000;
break;
case "minus":
all_time -=
days * 24 * 60 * 60 * 1000 +
hours * 60 * 60 * 1000 +
minutes * 60 * 1000 +
seconds * 1000;
break;
}
// 转换时间戳格式
return formatDate(all_time);
}

//时间戳转换方法yyyy-MM-dd HH:mm:ss(时间戳)
function formatDate(time) {
let date = new Date(time);

let YY = date.getFullYear();
let MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
let DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
let hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
let mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
let ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

// 这里可以修改返回的日期格式
return YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss;
}
/************************时间函数 结束******************************************/

posted on   若灵思源  阅读(1069)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示