Dayjs
一、day.js 常见方法
Section titled “一、day.js 常见方法”1.初始化时间
Section titled “1.初始化时间”- value为空的话获取当前时间
- value类型
- 时间戳
- 字符串
- Date的实例对象
dayjs(value).format('YYYY-MM-DD HH:mm:ss')2.获取时间戳
Section titled “2.获取时间戳”-
毫秒:自Unix纪元以来的毫秒数
dayjs('2019-01-25').valueOf()+dayjs(1548381600000) -
秒:自Unix纪元以来的秒数
秒要使用
unix()方法解析dayjs('2019-01-25').unix() // 1548381600//秒的时间戳dayjs().unix(1548381600)
3. 获取时间
Section titled “3. 获取时间”| 单位 | 缩写 | 描述 |
|---|---|---|
| week | w | 周 |
| day | d | 星期(星期日0,星期六6) |
| date | D | 天 |
| month | M | 月份(0-11) 记得+1 |
| year | y | 年 |
| hour | h | 小时 |
| minute | m | 分钟 |
| second | s | 秒 |
| millisecond | ms | 毫秒(默认输出单位) |
获取时间-缩写-关键字-方法
Section titled “获取时间-缩写-关键字-方法”//关键字获取dayjs().get("date")
//可以直接get(缩写)dayjs().get("y")
//也可以直接调用方法dayjs().year()//季度 此功能依赖QuarterOfYear插件dayjs('2010-04-01').quarter()dayjs().month()dayjs().date()dayjs().hour()//接受0到59的数字。如果超过这个范围,将持续到小时。dayjs().minute()dayjs().second()dayjs().millisecond()4. 获取当月天数
Section titled “4. 获取当月天数”dayjs('2019-01-25').toDate()5. 转对象 toObject()
Section titled “5. 转对象 toObject()”此功能依赖ToObject插件
dayjs('2019-01-25').toObject()6. 时间判断
Section titled “6. 时间判断”-
isAfter()/isBefore()/isSame判断一个日期是否在另外一个日期之前或者之后 或者相等 isAfter / isBefore / isSame
console.log('isAfter', dayjs('2022-04-20').isAfter(dayjs('2022-04-20'))) // 相同也为falseconsole.log('isBefore', dayjs('2022-04-20').isBefore(dayjs('2022-04-20'))) // 日期相同时也为falseconsole.log('isSame', dayjs('2022-04-20').isSame(dayjs('2022-04-20'))) // true-
判断一个日期是否在两个日期之间
isBetween()需要依赖
IsBetween插件,需要的时候查文档
7.是否是闰年
Section titled “7.是否是闰年”dayjs('2000-01-01').isLeapYear() // true8.计算时间
Section titled “8.计算时间”dayjs().add(7, 'day')//也可以使用负数
dayjs().add(-1, 'hour')dayjs().subtract(7, 'year')const start = new Date();const end = new Date();end.setDate(end.getDate()+1)//start - end = -1console.log(dayjs(start).diff(end, "day"));开始时间/结束时间
Section titled “开始时间/结束时间”//例:当前月份 开始 从1号开始console.log(dayjs().startOf("M").format("YYYY年 MM月 DD日 hh时 mm分 ss秒 ms毫秒"))
//例:当前月份 结束 30号 或者 31号console.log(dayjs().endOf("M").format("YYYY年 MM月 DD日 hh时 mm分 ss秒 ms毫秒"))二、插件安装
Section titled “二、插件安装”dayjs官方文档 > 安装 > 浏览器 > cdn资源 > jsDeilvr
.fromNow() - 从现在开始的时间
Section titled “.fromNow() - 从现在开始的时间”需要依赖:relativeTime 插件), 获取指定时间到现在的时间。
1. 下载插件 relativeTime插件
Section titled “1. 下载插件 relativeTime插件”dayjs官方文档 > 安装 > 浏览器 > cdn资源 > jsDeilvr >
/npm/dayjs@1.11.3/plugin
默认打印的是 英文的可以使用国际化插件, 转换成中文
<script src="../Dayjs-lib/libs/dayjs.js"></script>//导入插件js文件<script src="../Dayjs-lib/libs/dayjs.relative-time.js"></script><script>
//安装 dayjs.extend(dayjs_plugin_relativeTime) //设置时间 var s = dayjs().year(1998) //24 years ago console.log(s.fromNow()) //24 years 去掉后坠 console.log(s.fromNow(true))
</script>国际化 (i18n)
Section titled “国际化 (i18n)”1. 加载语言配置 (浏览器),下载插件
Section titled “1. 加载语言配置 (浏览器),下载插件”按需加载
-
cdn:方式
<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script><script src="https://unpkg.com/dayjs@1.8.21/locale/zh-cn.js"></script><script>dayjs.locale('zh-cn')</script> -
官网下载
dayjs官方文档 > 安装 > 浏览器 > cdn资源 > jsDeilvr >
/npm/dayjs@1.11.3/locale>/npm/dayjs@1.11.3/locale/zh-cn.js//使用方法同cdn使用指定的关键字加载国际化语言,按需加载语言文件。dayjs.locale('zh-cn') // 全局使用dayjs().locale('zh-cn').format() // 当前实例使用