Day.js的使用

7 阅读1分钟

day.js方便了我们对时间进行操作。 接下来我浅记录一下我对其的应用,后续如有新的学习会进行补充。

安装

npm install dayjs

基本用法

import dayjs from 'dayjs'; //先引入
const now = dayjs(); //获取当前时间
now.format('YYYY-MM-DD'); // 格式化输出当前日期为2023-10-01
const tomorrow = dayjs().add(1, 'day'); //当前时间加一天
tomorrow.format('YYYY-MM-DD'); // 输出明天的日期

本地化支持

import 'dayjs/locale/zh-cn'; // 引入中文本地化 
dayjs.locale('zh-cn'); // 设置为中文
dayjs().format('MM-DD ddd')//05-11 周日