卖坚果的怪叔叔 卖坚果的怪叔叔
  • 首页
  • 生活
  • 技术
  • 记录
  • 工具箱
  • 关于我
首页 › 分享 › Js时间小方法

Js时间小方法

坚果大叔
2024-01-29 16:40:06分享阅读 3,639
const formatPast = (date, type = "default", zeroFillFlag = true) => {
    const getTimeDifference = (current, past) => Math.floor((current - past) / 1000);
    const formatDate = (date, separator = "", zeroFill = true) => {
        const year = date.getFullYear();
        const month = zeroFill ? String(date.getMonth() + 1).padStart(2, "0") : date.getMonth() + 1;
        const day = zeroFill ? String(date.getDate()).padStart(2, "0") : date.getDate();
        return ${year}${separator}${month}${separator}${day};
    };
    const timeDifference = getTimeDifference(new Date(), new Date(date));
    if (timeDifference < 10) {
        return "刚刚";
    } else if (timeDifference < 60) {
        return ${timeDifference}秒前;
    } else if (timeDifference < 3600) {
        return ${Math.floor(timeDifference / 60)}分钟前;
    } else if (timeDifference < 86400) {
        return ${Math.floor(timeDifference / 3600)}小时前;
    } else if (timeDifference >= 86400 && type === "default") {
        const daysDifference = Math.floor(timeDifference / 86400);
        if (daysDifference >= 365) {
            return ${Math.floor(daysDifference / 365)}年前;
        } else if (daysDifference >= 30) {
            return ${Math.floor(daysDifference / 30)}个月前;
        } else {
            return ${daysDifference}天前;
        }
    } else {
        const formattedDate = formatDate(new Date(date), type === "年月日" ? "年月日" : type);
        return zeroFillFlag ? formattedDate : formattedDate.replace(/^[0]+/g, "");
    }
};
console.log(formatPast("2024-1-1 11:11:11")); // 3天前
console.log(formatPast("2023-11-1 11:11:11")); // 2个月前
console.log(formatPast("2015-07-10 21:32:01")); // 8年前
console.log(formatPast("2023-02-01 09:32:01", "-", false)); // 2023-2-1
console.log(formatPast("2023.12.8 19:32:01", "/")); // 2023/12/08
console.log(formatPast("2023.12.8 19:32:01", ".")); // 2023.12.08
console.log(formatPast("2023/5/10 11:32:01", "年月日")); // 2023年05月10日
console.log(formatPast("2023/6/25 11:32:01", "月日", false)); // 6月25日
console.log(formatPast("2023/8/08 11:32:01", "年")); // 2023年
赞赏 赞(1)
记一次网站莫名其妙的问题。
上一篇
上海野生动物园~
下一篇
在小程序中查看

这是个😊,挣点服务器费用😜

热门文章
Python获取好友地区分布及好友性别分布
链式取值(转载整理)
iphone 利用 Scriptable 添加网上国网电费小组件
娃上幼儿园了。
我用Trae做了一个一键同步热点要闻到公众号的工具
Python基础教程之字符串和编码
专题标签
agent (1) CSS (15) dom (1) Es6入门系列 (14) Git (3) html (1) iview (1) JavaScript (9) js (4) Lif (1) Node (4) pyecharts (1) Python (10) ref (1) scriptable (1) Skill (1) vsCode (1) vue (4) wordpress (2) 三月 (1) 上海 (1) 假期 (1) 其他分享 (3) 前端两三问 (15) 古镇 (1) 外滩 (1) 对象 (1) 小程序 (5) 年度总结 (6) 年终总结 (1) 微信机器人 (4) 打字机 (1) 技术 (1) 日常问题 (8) 日常问题记录 (1) 春天 (2) 服务器 (2) 海族馆 (1) 生活 (8) 网上国网 (1) 记录 (3) 通勤 (1) 重学JavaScript (11) 面试题 (10) 🌸 (1)
免费领取一年Gemini Pro使用权益!!
2026-01-14 19:34:11
4,067 0
腾讯视频VIP会员免费领取!3-31天。
2025-07-16 20:55:40
3,986 1
吐血收集的1000+九号电动车提示音,免费领取!
2025-07-09 21:29:40
10,558 2
PC微信多开和防撤回~
2025-02-25 20:51:26
5,384 0
1
  • 1
博主

一位佛系的前端开发者,略通摄影,乐于尝试新事物,热衷于美食。

友链
故事胶片
公众号
坚果大叔 执行上下文 卖坚果的怪叔叔 Dacking
Copyright © 2017-2026 卖坚果的怪叔叔

开往-友链接力

萌ICP备20230818号

苏ICP备18048410号-3
  • 首页
  • 生活
  • 技术
  • 记录
  • 工具箱
  • 关于我
# CSS # # JavaScript # # vue # # 微信 # # 生活 #
坚果大叔
345
文章
170
评论
431
喜欢