Day.js 常用方法 以后再也不用new Date( )啦

一、为什么使用Day.js

文件大小只有2KB左右,下载、解析和执行的JavaScript更少,为代码留下更多的时间。而且更好的支持node、浏览器、小程序、ts 的各种环境。

如果这时候让你手写一个获取当前时间  

 getNowDay() {
        var myDate=new Date;
        var year=myDate.getFullYear(); //获取当前年
        var mon=myDate.getMonth()+1<10?"0"+(myDate.getMonth()+1):myDate.getMonth()+1; //获取当前月
        var date=myDate.getDate()<10?"0"+myDate.getDate():myDate.getDate(); //获取当前日
        var hour=myDate.getHours()<10?"0"+myDate.getHours():myDate.getHours();//获取当前时
        var minute=myDate.getMinutes()<10?"0"+myDate.getMinutes():myDate.getMinutes();//获取当前分
        return year+"-"+mon+"-"+date+" "+hour+":"+minute+":00";
    },

如果使用了 Day.js获取当前时间

dayjs().format("YYYY-MM-DD HH:mm:ss")

 我们利用手写的时间 去学习和研究一些其他的功能 岂不是乐乐而不为 如果你是小白 还是建议你先学习手写哈

二、安装

npm install dayjs --save

然后在项目代码中引入即可:

var dayjs = require('dayjs')
// import dayjs from 'dayjs' // ES 2015
dayjs().format()

三、常用方法

1. 获取当前时间

console.log(dayjs()); //获取Day.js的当前时间对象
console.log(dayjs().format("YYYY-MM-DD")); // 2022-11-18
console.log(dayjs().format("YYYY-MM-DD HH:mm:ss"));// 2022-11-18 19:42:13

2.传入时间戳转换当前时间

console.log(dayjs(1668739461501).format("YYYY-MM-DD")); // 2022-11-18
console.log(dayjs(1668769665501).format("YYYY-MM-DD HH:mm:ss")); // 2022-11-18 19:07:45

3.转换格式 

console.log(dayjs('2022-11-18').format('YYYY/MM/DD') );// '2022/11/18'
console.log(dayjs('2022-11-18').format('DD/MM/YYYY')); // '18/11/2022'

4.校验是否是日期格式

console.log(dayjs("2022-01-33").isValid()); // true
console.log(dayjs("some invalid string").isValid()); //false

5.严格校验

console.log(dayjs('2022-02-31', 'YYYY-MM-DD', true).isValid()); // true
console.log(dayjs('2022-02-31 19:16', 'YYYY-MM-DD', true).isValid()); // true
console.log(dayjs('31-02-2022', 'YYYY-MM-DD', true).isValid()); // false

 6.时间 加/减

n代表天数  dayjs(value) 可以传入日期 不传默认当天

console.log(dayjs().add(n, 'day').format("YYYY-MM-DD")); // 在当前时间加1天
console.log(dayjs().subtract(n, 'day').format("YYYY-MM-DD")); //在当前时间减1天

获取当前日期的后7天 (前7天同理替换subtract方法,常用于购买车票、预约等)

let afterDate = []
for (let i = 1; i <= 7; i++) {
   afterDate.push(dayjs().add(i, 'day').format("YYYY-MM-DD"))
}
console.log(afterDate);

常用格式:

输入 示例 描述
YY 18 两位数的年份
YYYY 2018 四位数的年份
M 1-12 月份,从 1 开始
MM 01-12 月份,两位数
MMM Jan-Dec 缩写的月份名称
MMMM January-December 完整的月份名称
D 1-31 月份里的一天
DD 01-31 月份里的一天,两位数
H 0-23 小时
HH 00-23 小时,两位数
h 1-12 小时, 12 小时制
hh 01-12 小时, 12 小时制, 两位数
m 0-59 分钟
mm 00-59 分钟,两位数
s 0-59
ss 00-59 秒,两位数
S 0-9 毫秒,一位数
SS 00-99 毫秒,两位数
SSS 000-999 毫秒,三位数

 7.时间差异  

ps: 下次判断开始时间大于结束时间 不要再说不会了

const date1 = dayjs('2022-11-18')
const date2 = dayjs('2022-11-17')
console.log( date1.diff(date2)); // 86400000 默认单位是毫秒

 第二个参数可以传入单位  例如月份 判断相差多少个月 年份 相差多少年 

相差多少月

const date4 = dayjs('2019-09-25')
console.log(date4.diff('2019-06-05', 'month')); // 7

相差多少年

const date5 = dayjs('2019-06-25')
console.log(date5.diff('2019-06-05', 'day')); // 20

 8.判断两个日期是否相等

console.log(dayjs('2022-11-17').isSame(dayjs('2022-11-18'))); // false 默认毫秒

当使用第二个参数时,将会连同去比较更大的单位。

console.log(dayjs('2011-05-01').isSame('2011-01-01', 'year')); //true 

常用单位:

单位 缩写 描述
day d
week w
month M 月份(0-11)
year y
hour h 小时
minute m 分钟
second s
millisecond ms 毫秒

本文列举了一些 工作中常用的方法作参考  你想了解更多可以去 Day.js 官网

猜你喜欢

转载自blog.csdn.net/m0_46846526/article/details/127916909