Day.js 是一个轻量的处理时间和日期的 JavaScript 库:Day.js中文网 (fenxianglu.cn)
只要导入了 dayjs的库文件,在window全局就可以使用dayjs()的方法了
先new出来一个 date()的对象,再进行打印
let time = new Date()
console.log(time);
------------------------------
Thu Jan 19 2023 16:01:14 GMT+0800 (中国标准时间)
得到一个不适合阅读的字符串
这个时候可以使用 Day.js对Date()进行处理
我们通常需要对时间进行一个:YY-MM-DD HH:mm:ss 的可视化处理
1、当前时间 dayjs()
直接调用 dayjs() 将返回一个包含当前日期和时间的 Day.js 对象。
// 1、直接调用dayjs()方法获取的是当前时间
console.log(dayjs());
等同于 dayjs(new Date()) 的调用。
dayjs()方法可以传参,
dayjs(给定的日期时间)得到指定的日期
let time = new Date()
dayjs(time)
2、dayjs() + format() 格式化当前时间
使用:dayjs()方法拼接format()方法可以格式化指定的时间
例如:
let time = new Date()
//1、格式化年份
console.log(dayjs(time).format('YYYY')) // 2023 (YYYY 四月份的年份,YY则是23)
//2、格式化月份
console.log(dayjs(time).format('MM')) // 01 (MM-两位数的月份)
//3、格式化 年-月-日
console.log(dayjs(time).format('YYYY-MM-DD')) // 2023-01-19
console.log(dayjs(time).format('YYYY年MM月DD日')) // 2023年01月19日
//4、格式化 年月日时分秒:YYYY-MM-DD HH:mm:ss
console.log(dayjs(time).format('YYYY-MM-DD HH:mm:ss')) // 2023-01-19 16:25:23
输入示例描述YY18两位数的年份YYYY2018四位数的年份M1-12月份,从 1 开始MM01-12月份,两位数MMMJan-Dec缩写的月份名称MMMMJanuary-December完整的月份名称D1-31月份里的一天DD01-31月份里的一天,两位数H0-23小时HH00-23小时,两位数h1-12小时, 12 小时制hh01-12小时, 12 小时制, 两位数m0-59分钟mm00-59分钟,两位数s0-59秒ss00-59秒,两位数
推荐阅读
发表评论