Day.js 常用用法

本文详细展示了如何使用Day.js库进行时间戳转换、年月日操作、星期计算以及时间比较等,包括获取当前时间、转换为时间戳、获取月份天数、比较时间差等,适用于前端开发中的日期处理场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ㅤㅤㅤ
ㅤㅤㅤ
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ(我认为克服恐惧最好的办法理应是:面对内心所恐惧的事情,勇往直前地去做,直到成功为止。——罗斯福)
ㅤㅤㅤ
ㅤㅤㅤ
ㅤㅤㅤㅤㅤㅤㅤㅤㅤ在这里插入图片描述

Day.js

时间戳转换
const nowTime = dayjs().format();
console.log('获取当前时间', nowTime);

const nowSecondTimestamp = dayjs(nowTime).unix();
console.log('当前时间转换为秒级时间戳', nowSecondTimestamp);

const nowMillisecondTimestamp = dayjs(nowTime).valueOf();
console.log('当前时间转换为毫秒级时间戳', nowMillisecondTimestamp);

const nowTimeFormat = dayjs(nowTime).format(defaultFormat);
console.log('使用指定格式转换时间', nowTimeFormat);
年份操作
const thisYear = dayjs(nowTime).format('YYYY');
console.log('获取当前年份', thisYear);

const preYear = dayjs(nowTime).subtract(1, 'year').format('YYYY');
console.log('获取现在时间的上一个年份', preYear);

const targetPreYear = dayjs(preYear).subtract(1, 'year').format('YYYY');
console.log('获取指定年份的上一年', targetPreYear);
月份操作
const thisMonth = dayjs(nowTime).format('MM');
console.log('获取当前月份', thisMonth);

const preMonth = dayjs(nowTime).subtract(1, 'month').format('MM');
console.log('获取上个月份', preMonth);

const thisMonthDays = dayjs(nowTime).daysInMonth();
console.log('获取当天月份天数', thisMonthDays);

const preMonthDays = dayjs(nowTime).subtract(1, 'month').daysInMonth();
console.log('获取上个月份天数', preMonthDays);

const targetMonthDays = dayjs(preMonth).daysInMonth();
console.log('获取指定月份天数', targetMonthDays);

const thisMonthFirstDays = dayjs(nowTime).startOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第一天', thisMonthFirstDays);

const thisMonthEndDays = dayjs(nowTime).endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('当月最后一天', thisMonthEndDays);

const thisMonthFirstDaysBeforce7Days = dayjs(thisMonthFirstDays).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第一天前七天', thisMonthFirstDaysBeforce7Days);

const thisMonthEndDaysBeforce7Days = dayjs(thisMonthEndDays).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月月底前七天', thisMonthEndDaysBeforce7Days);

const thisMonthFirstDaysAfter7Days = dayjs(thisMonthFirstDays).subtract(-7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第一天后七天', thisMonthFirstDaysAfter7Days);

const thisMonthEndDaysAfter7Days = dayjs(thisMonthEndDays).subtract(-7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月月底后七天', thisMonthEndDaysAfter7Days);

const preMonthSecondDay = dayjs(nowTime).subtract(1, 'month').startOf('month').subtract(-2, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('上个月第二周周一', preMonthSecondDay);

const thisMonthSecondDay = dayjs(nowTime).startOf('month').subtract(-2, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第二周周一', thisMonthSecondDay);

const nextMonthSecondDay = dayjs(nowTime).subtract(-1, 'month').startOf('month').subtract(-2, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('下个月第二周周一', nextMonthSecondDay);

const targetMonthStartTime = dayjs('2021-02').startOf('month').format('YYYY-MM-DD HH:mm:ss');
const targetMonthEndTime = dayjs('2021-02').endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('指定月份的开始和结束时间', targetMonthStartTime, targetMonthEndTime);

const monthDiff = dayjs(nowTime).diff('2021-01', 'month', true);
console.log('比较两个时间相差月份', monthDiff);

const nextMonthFirstDay = dayjs(nowTime).add(1, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('获取下个月第一天', nextMonthFirstDay);

const nextMonthLastDay = dayjs(nowTime).add(1, 'month').endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('获取下个月最后一天', nextMonthLastDay);
星期操作
const thisDateWeek = dayjs(nowTime).day();
console.log('获取当前时间是周几 星期', thisDateWeek);

const thisMonthFirstWeekStartTime = dayjs(nowTime).startOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss');
const thisMonthFirstWeekEndTime = dayjs(nowTime).endOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('本周开始和结束时间', thisMonthFirstWeekStartTime, thisMonthFirstWeekEndTime);

const thisMonthFirstMonthStartTime = dayjs(nowTime).startOf('month').format('YYYY-MM-DD HH:mm:ss');
const thisMonthFirstMonthEndTime = dayjs(nowTime).endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('本月开始和结束时间', thisMonthFirstMonthStartTime, thisMonthFirstMonthEndTime);

// 比较两个时间相差星期
const weekDiff = dayjs(nowTime).diff('2021-01', 'week', true);
console.log('比较相差的星期', weekDiff);

const preeWeek = dayjs(nowTime).subtract(1, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('一周前的时间', preeWeek);

// 在这里需要加2天 是因为 dayjs的星期天是从0开始
const preeTuesday = dayjs(nowTime).subtract(1, 'week').startOf('week').add(2, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('上周二', preeTuesday);

const thisTuesday = dayjs(nowTime).startOf('week').add(2, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('本周二', thisTuesday);

const nextTuesday = dayjs(nowTime).startOf('week').add(1, 'week').add(2, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('下周二', nextTuesday);
指定天操作
// 比较两个时间相差的天数
const dayDiff = dayjs(nowTime).diff('2021-01', 'day', true);
console.log('比较相差的天数', dayDiff);

const beforeTenDays = dayjs(nowTime).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('查看七天前的日期', beforeTenDays);

// 时分秒操作

const isBeforeDate = dayjs('2021-02-01').isBefore('2021-01-01');
console.log('是否在指定时间之前', isBeforeDate);

const isAfter = dayjs('2021-02-01').isAfter('2021-01-01');
console.log('是否在指定时间之后', isAfter);

const isSame = dayjs('2021-02-01').isSame('2021-02-01');
console.log('是否相同时间', isSame);

// 时间校验

console.log('校验是否争取的时间格式', dayjs('202112312asdxzsc').isValid());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值