«

为什么选择Day.js?比Moment.js更轻更快的日期处理神器

cola 发布于 阅读:58 JavaScript


在JavaScript开发中,处理日期和时间常常让人头大。过去我们多依赖
Moment.js,但随着ES模块与新项目对性能的苛刻要求,Day.js以“极简API、2k体积、与Moment高度兼容”成为新宠。本文带你从入门到实战,系统掌握Day.js!


1. 安装与入门

安装

    npm install dayjs
    <!-- CDN example (jsDelivr) --> <script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script> <script>dayjs().format()</script>

快速体验

    const dayjs = require('dayjs'); // Node环境  
    console.log(dayjs().format()); //当前时间 例如: 2025-05-30T09:55:02+08:00

2. Day.js的基本用法

2.1 创建日期对象

    dayjs();                            // 当前时间  
    dayjs('2024-06-01');                // 指定日期  
    dayjs('2024-06-01 12:00', 'YYYY-MM-DD HH:mm'); // 指定格式  
    dayjs(1716816000000);               // 时间戳(毫秒)

2.2 格式化日期

    dayjs().format('YYYY-MM-DD HH:mm:ss'); // 2025-05-30T09:55:02+08:00  
    dayjs('2024-06-01').format('dddd');    // Saturday

2.3 取日期各项

    dayjs().year();  // 2024  
    dayjs().month(); // 0~11,0为1月  
    dayjs().date();  // 几号  
    dayjs().hour();  // 小时  
    dayjs().minute(); // 分  
    dayjs().second(); // 秒  
    dayjs().day();   // 星期几(0=周日)

3. 日期计算

3.1 加减时间

    dayjs().add(7, 'day').format('YYYY-MM-DD');    // 加7天  
    dayjs().subtract(1, 'month').format('YYYY-MM'); // 减1月

3.2 日期差值

    const start = dayjs('2024-06-01');  
    const end = dayjs('2024-06-20');  
    end.diff(start, 'day');   // 19  
    end.diff(start, 'month'); // 0

支持单位: 'year', 'month', 'week', 'day', 'hour', 'minute', 'second'

3.3 判断日期

    const d1 = dayjs('2024-06-01');  
    const d2 = dayjs('2024-07-01');  
    d1.isBefore(d2);    // true  
    d1.isAfter(d2);     // false  
    d1.isSame('2024-06-01', 'day'); // true

4. 进阶玩法——解析与插件系统

Day.js核心功能“够用即止”,更多高级需求需“插件”支持。

4.1 本地化(多语言)

    const dayjs = require('dayjs');  
    const localizedFormat = require('dayjs/plugin/localizedFormat');  
    const localeLan = require('dayjs/locale/zh-cn');  
    dayjs.extend(localizedFormat); // 加载本地化格式插件  
    dayjs.locale(localeLan); // 使用中文  

    console.log(dayjs().format('LLLL')); // 2025年5月30日星期五上午10点27分

format 格式可以参考https://day.js.org/docs/zh-CN/display/format

4.2 定制解析与显示

    const customParseFormat = require('dayjs/plugin/customParseFormat');  
    dayjs.extend(customParseFormat);  

    const d = dayjs('2024年06月01日', 'YYYY年MM月DD日');  
    console.log(d.format('YYYY-MM-DD')); // 2024-06-01

4.3 时间区间(isBetween)

    const isBetween = require('dayjs/plugin/isBetween');  
    dayjs.extend(isBetween);  

    let now = dayjs('2024-06-10');  
    console.log(now.isBetween('2024-06-01', '2024-06-20')); // true

4.4 相对时间(fromNow)

    const dayjs = require('dayjs');  
    const localizedFormat = require('dayjs/plugin/localizedFormat');  
    const localeLan = require('dayjs/locale/zh-cn');  
    const relativeTime = require('dayjs/plugin/relativeTime');  
    dayjs.extend(localizedFormat); // 加载本地化格式插件  
    dayjs.locale(localeLan); // 使用中文  
    dayjs.extend(relativeTime);  

    console.log(dayjs().subtract(1, 'day').fromNow()); // "1天前"  
    console.log(dayjs().add(2, 'hour').fromNow());     // "2小时内"

5. 总结


引用链接

[1] 官方插件列表: https://day.js.org/docs/zh-CN/plugin/plugin

作者头像