一、 前言
Day.js是一个轻量级的JavaScript日期处理库,体积小巧,功能实用,包括日期格式化、加减操作、时间截取、时间差计算等。
二、安装使用
安装
npm install dayjs
# 或者
yarn add dayjs
使用
<script setup>
import dayjs from 'dayjs'
const formattedDate = dayjs().format('YYYY-MM-DD HH:mm:ss')
console.log(formattedDate)
</script>
三、常用方法及参数详解
有毒,解读后食用
1.dayjs() 时间对象
- 下面是几个
比较常用的传入格式,统一返回时间对象
dayjs()等同于dayjs(new Date())
dayjs();
dayjs(new Date());
dayjs(1686873600000);
dayjs('2025-05-12');
dayjs('2025/05/12 15:35:04');
dayjs('2025-05-12T10:22:30Z');
dayjs('2023-06-15T14:22:30+08:00')
2. format() 格式化时间日期
1、Y 年
| 参数 | 说明 | 示例输出 |
|---|
YY | 2位数年份 | "25" |
YYYY | 4位数年份 | "2025" |
dayjs().format('YY年');
dayjs().format('YYYY');
dayjs('2026-09-22').format('YYYY');
2、M 月
| 参数 | 说明 | 示例输出 |
|---|
M | 数字月份(1-12) | "5" |
MM | 两位数月份 | "05" |
dayjs().format('M月');
dayjs().format('MM');
dayjs('2025-12-11').format('M');
window.location.href = "https://juejin.cn/user/84036866547575"
3、D 日
| 参数 | 说明 | 示例输出 |
|---|
D | 月份中的日期(1-31) | "1" |
DD | 两位数日期 | "01" |
dayjs().format('D日');
dayjs().format('DD');
dayjs('2025-07-11').format('D');
4、H/h 时
| 参数 | 说明 | 示例输出 |
|---|
H | 24小时制(0-23) | "14" |
HH | 两位数24小时制 | "14" |
h | 12小时制(1-12) | "2" |
hh | 两位数12小时制 | "02" |
dayjs().format('H');
dayjs('2025-05-12 14:22:10').format('hh');
5、m/s 分/秒
| 参数 | 说明 | 示例输出 |
|---|
m | 分钟(0-59) | "32" |
mm | 两位数分钟 | "32" |
s | 秒(0-59) | "45" |
ss | 两位数秒 | "45" |
dayjs().format('m');
dayjs('2025-05-12 14:32:45').format('ss');
6、d 星期
| 参数 | 说明 | 示例输出 |
|---|
d | 数字星期(0-6, 0是周日) | "6" |
// const weekStr = ['日', '一', '二', '三', '四', '五', '六']
const weekStr = '日一二三四五六'
const week = dayjs().format('d') || 0
console.log(week)
console.log('星期' + weekStr[week])
const week1 = dayjs('2025-05-17').format('d') || 0
console.log(week1)
console.log('星期' + weekStr[week1])
6、A/a 上下午
| 参数 | 说明 | 示例输出 |
|---|
A | 大写的AM/PM | "PM" |
a | 小写的am/pm | "pm" |
const upOrDown = dayjs().format('a');
const upOrDown1 = dayjs('2025.5.12 13:25:14').format('A');
console.log(upOrDown.replace('am', '上午').replace('pm', '下午'));
console.log(upOrDown1.replace('AM', '上午').replace('PM', '下午'));
window.location.href = "https://juejin.cn/user/84036866547575"
import 'dayjs/locale/zh-cn';
dayjs.locale('zh-cn');
const upOrDown = dayjs().format('a');
const upOrDown1 = dayjs('2025.5.12 13:25:14').format('A');
console.log(upOrDown);
console.log(upOrDown1);
// 根据区间判定问候语
// time为可选参数,参数类型等同于dayjs()
function getTimePeriod(time) {
const hour = dayjs(time).hour();
if (hour >= 0 && hour < 5) {
return "凌晨";
} else if (hour >= 5 && hour < 9) {
return "早上";
} else if (hour >= 9 && hour < 11) {
return "上午";
} else if (hour >= 11 && hour < 13) {
return "中午";
} else if (hour >= 13 && hour < 17) {
return "下午";
} else if (hour >= 17 && hour < 19) {
return "傍晚";
} else {
return "晚上";
}
window.location.href = "https://juejin.cn/user/84036866547575"
}
console.log(`${getTimePeriod()}好`); //中午好
7、X/x 时间戳
| 参数 | 说明 | 示例输出 |
|---|
X | Unix秒时间戳 | "1680307200" |
x | Unix毫秒时间戳 | "1680307200000" |
console.log(dayjs().format('x'),'毫秒级时间戳');
console.log(dayjs('2025.5.12 13:25:14').format('X'),'秒级时间戳');
3. add() / subtract() 增加/减少
基本使用方法
dayjs().add(数量, 单位)
dayjs().subtract(数量, 单位)
数量参数说明
- 常规使用为
正整数,0返回原值;
- 支持小数,
部分单位适用,使用某些单位时会对数量参数进行四舍五入转换,可能影响预期结果,慎用!;
- 支持负数,如:
add(-1, 'day') 等同于 subtract(1, 'day')
- 上述几种参数均支持
string 与 number 类型数字
单位参数说明
| 单位 | 缩写 | 说明 |
|---|
millisecond | ms | 毫秒 |
second | s | 秒 |
minute | m | 分钟 |
hour | h | 小时 |
day | d | 天 |
week | w | 周 |
month | M | 月份 |
quarter | Q | 季度(需要插件) |
year | y | 年 |
使用示例
常规用法
dayjs('2023-04-01').add(7, 'day')
dayjs('2023-04-01 12:00').subtract(3, 'hour')
小数用法
dayjs('2023-04-01 12:00').add(1.5, 'hour')
dayjs('2023-04-01 12:00').subtract(0.5, 'day')
批量处理
// 批量处理 加5天
const dates = ['2023-04-01', '2023-05-01', '2023-06-01']
const adjustedDates = dates.map(d => dayjs(d).add(5, 'day').format('YYYY-MM-DD'))
console.log(adjustedDates)
window.location.href = "https://juejin.cn/user/84036866547575"
4. 其他常规用法
获取时间单位 常用单位
dayjs().year()
dayjs().month()
dayjs().date()
dayjs().hour()
dayjs().minute()
dayjs().second()
dayjs().day()
设置时间单位 链式 / set
console.log(date.year(2025).month(11).format('YYYY-MM-DD'));
console.log(date.set('month', 8).format('YYYY-MM-DD'));
相差天数 diff()
console.log(dayjs('2025-08-15').diff(dayjs(), 'day'));
比较时间 isAfter() / isBefore() / isSame()
console.log(dayjs().isAfter('2023-08-02'));
console.log(dayjs().isBefore('2025-01-22'));
console.log(dayjs('2025-05-15').isSame(dayjs(), 'month'));
查询相关 startOf() / endOf() / daysInMonth()
参数:
-
unit(字符串):时间单位,支持以下值:
'year' → 年
'month' → 月
'week' → 周
'day' → 天
'hour' → 小时
'minute' → 分钟
'second' → 秒
当年第一天(即某年-01-01)
console.log(dayjs().startOf('year').format('YYYY-MM-DD'));
本周最后一天(即本周日)
console.log(dayjs().endOf('week').format('YYYY-MM-DD'));
本月倒数第三天(适用于月初月末提醒等场景)
console.log(dayjs().endOf('month').subtract(2, 'day').format('YYYY-MM-DD'));
当月天数(以当前时间五月为例,则返回31)
console.log(dayjs().daysInMonth());
window.location.href = "https://juejin.cn/user/84036866547575"
当年天数(平年365 闰年366)
const yearEnd = dayjs().endOf('year');
const yearStart = dayjs().startOf('year');
console.log(yearEnd.diff(yearStart, 'day') + 1);