Day.js是一个极简的JavaScript库,可以为现代浏览器解析、验证、操作和显示日期和时间。
NPM:
npm install dayjs --save
import dayjs from ‘dayjs’
// 或者 CommonJS
// var dayjs = require(‘dayjs’);
dayjs().format();
CDN:
<script src="https://unpkg.com/dayjs"></script>
<script>
dayjs().format();
</script>
常用方法
当前时间
dayjs()
时间字符串
dayjs(‘2018-06-03’)时间戳
dayjs(1528361259484)Date 对象
dayjs(new Date(2018,8,18))复制
dayjs().clone()检测当前 Dayjs 对象是否是一个有效的时间
dayjs().isValid()获取
年 : dayjs().year()
月 : dayjs().month()
日 : dayjs().date()
星期 : dayjs().day()
时 : dayjs().hour()
分 : dayjs().minute()
秒 : dayjs().second()
毫秒 : dayjs().millisecond()设置
dayjs().set(‘year’,2017)
dayjs().set(‘month’,9)增加时间并返回一个新的 Dayjs() 对象
dayjs().add(7, ‘day’)
dayjs().add(7, ‘year’)减少时间并返回一个新的 Dayjs() 对象
dayjs().subtract(7, ‘year’)
dayjs().subtract(7, ‘month’)返回当前时间的开头时间的 Dayjs() 对象,如月份的第一天。
dayjs().startOf(‘year’)
dayjs().startOf(‘month’)返回当前时间的末尾时间的 Dayjs() 对象,如月份的最后一天。
dayjs().endOf(‘month’)
dayjs().endOf(‘year’)格式化
dayjs().format()
dayjs().format(‘YYYY-MM-DD dddd HH:mm:ss.SSS A’)时间差
dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),‘years’)
dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),‘day’)
dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),‘hour’)dayjs(‘2018-06-08’).diff(dayjs(‘2017-06-01’),'sec)Unix 时间戳 (毫秒)
dayjs().valueOf()Unix 时间戳 (秒)
dayjs().unix()返回月份的天数
dayjs().daysInMonth()返回原生的 Date 对象
dayjs().toDate()返回包含时间数值的数组
dayjs().toArray()当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串
dayjs().toJSON() //2018-06-08T02:44:30.599Z返回 ISO8601 格式的字符串
dayjs().toISOString() //2018-06-08T02:46:06.554Z返回包含时间数值的对象
dayjs().toObject()字符串
dayjs().toString()检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前
dayjs(‘2018-06-01’).isBefore(dayjs(‘2018-06-02’))检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同
dayjs().isSame(dayjs())检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后
dayjs().isAfter(dayjs())
No. | 大剑师精品GIS教程推荐 |
---|---|
0 | 地图渲染基础- 【WebGL 教程】 - 【Canvas 教程】 - 【SVG 教程】 |
1 | Openlayers 【入门教程】 - 【源代码+示例 300+】 |
2 | Leaflet 【入门教程】 - 【源代码+图文示例 150+】 |
3 | MapboxGL 【入门教程】 - 【源代码+图文示例150+】 |
4 | Cesium 【入门教程】 - 【源代码+综合教程 200+】 |
5 | threejs 【中文API】 - 【源代码+图文示例200+】 |
6 | Shader 编程 【图文示例 100+】 |
7 | Geoserver 【配置教程 100+】 |
8 | 卫星应用开发教程 【配置+应用教程 100+】 |
9 | GIS数字孪生与大模型 【应用实战 100+】 |
10 | 报表与数字大屏 【Echarts 实战示例】 - 【D3 综合教程】 - 【其他大屏】 |