Dayjs

17 阅读1分钟

获取本月最后一天

注意:使用.format('YYYY-MM-DD') 获取转换后的格式

const dayjs = require('dayjs');

const lastDay = dayjs().endOf('month');

console.log(lastDay);
// 2023-03-31T23:59:59.999Z

const lastDay = dayjs().endOf('month').format('YYYY-MM-DD');
// 2023-11-30

查询某天前几个月的第一天

需求: 范围查询本月前4个月的某个月的第一天 如:2023-10-01 00:44:16 - 2023-11-24 16:44:20

const dayjs = require('dayjs');

const firstDay = dayjs().startOf('month');

console.log(firstDay);
// 2023-03-01T00:00:00.000Z

dayjs 获取几个月后日期

如 此时的3个月后

const dayjs = require('dayjs');

const nextMonth = dayjs().add(1, 'month');

console.log(nextMonth);
// 2023-04-01T00:00:00.000Z