dayjs是一个轻量的处理时间和日期的 JavaScript 库;
github地址;
文档地址
Moment 被设计为在浏览器和 Node.js 中都能工作,文档地址,使用方法大同小异
dayjs的优势:
1.和Moment.js有着相同的API和模式(Moment.js地址:http://momentjs.cn/);
2.不可变、持久性
3. 提供链式调用
4. 国际化标准
5.超小的压缩体积,仅仅有2kb左右(Moment.js压缩后大概有16.7k的样子)
6.极大多数的浏览器兼容
npm安装:
npm install dayjs --save
cdn引用:
dayjs相关API解析:
直接运行 dayjs(),得到包含当前时间和日期的 Dayjs 对象,类似new Date()
dayjs() // Thu Jul 16 2020 14:11:57 GMT
我在控制台的操作如下:
可以解析传入的一个标准的ISO 8601时间字符串
dayjs('2020-7-16') // 类上
可以解析传入的一个 Javascript Date 对象
dayjs(new Date(2020,7,16))
可以解析传入的一个 Unix 时间戳 (13 位数字)
dayjs(1594880342107) // Thu Jul 16 2020 14:19:34 GMT+0800 (中国标准时间) {}
Dayjs 对象是不可变的,如果您想获得一个对象的拷贝,请执行 .clone()。 向 dayjs() 里传入一个 Dayjs 对象也能实现同样的效果
dayjs(Dayjs)
// 或者
dayjs().clone()
检测当前 Dayjs 对象是否是一个有效的时间
dayjs().isValid()
dayjs获取和设置:
获取或设置年份
dayjs().year() // 2020
dayjs().year(2020)
dayjs().set('year',2020) // 2020
获取或设置月份。从 0 开始
dayjs().month() // 6 +1则是7月
dayjs()month(6) //
dayjs().set('month',6) // 7月
其他的日期/星期/时/分/秒等,参照下表

增加减少相关操作:
增加时间并返回一个新的 Dayjs() 对象
dayjs().add(value : Number, unit : String);
dayjs().add(7, 'day'); //在当前的基础上加7天
减少时间并返回一个新的 Dayjs() 对象
dayjs().subtract(value : Number, unit : String);
dayjs().subtract(7, 'day'); //在当前基础上减少7天
返回当前时间的开头时间的 Dayjs() 对象,如月份的第一天
dayjs().startOf(unit : String);
dayjs().startOf('month');
返回当前时间的末尾时间的 Dayjs() 对象,如月份的最后一天
dayjs().endOf(unit : String);
dayjs().endOf('month');
格式化 Dayjs 对象
dayjs().format(String)
dayjs('2020-07-16').format('YYYY-MM-DD HH:mm:ss')
dayjs().format('YYYY-MM-DD')
dayjs().format('YYYY-MM')

获取两个 Dayjs 对象的时间差,默认毫秒
const date1 = dayjs('2020-07-16')
const date2 = dayjs('2020-06-16')
date1.diff(date2) // 得到时间差的时间戳
date1.diff(date2, 'month') // 1
date1.diff(date2, 'month', true) // 精确到小数
date1.diff(date2, 'day') // 相差多少天
返回 Unix 时间戳 (毫秒)
dayjs().valueOf()
返回 Unix 时间戳 (秒)
dayjs().unix()
返回月份的天数
dayjs().daysInMonth()
返回原生的 Date 对象
dayjs().toDate()
当序列化 Dayjs 对象时,会返回 ISO8601 格式的字符串
dayjs().toJSON() //"2020-07-16T00:00:00.000Z"
dayjs().toISOString() // 还是返回ISO8601 格式的字符串
dayjs().toString() // 返回字符串
检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之前
dayjs().isBefore(Dayjs, unit? : String)
检查一个 Dayjs 对象是否和另一个 Dayjs 对象时间相同
dayjs().isSame(Dayjs, unit? : String)
检查一个 Dayjs 对象是否在另一个 Dayjs 对象时间之后
dayjs().isAfter(Dayjs, unit? : String)
取值/赋值:
dayjs().second(30).valueOf() // => new Date().setSeconds(30)
dayjs().second() // => new Date().getSeconds()
自定义:
可以创建一个新的语言配置,定义自己的dayjs,
var localeObject = {...} // Day.js 语言对象
dayjs.locale('en-my-settings', localeObject);
更新一个已有的语言配置,使用本功能需先配置 UpdateLocale 插件,才能正常运行
dayjs.extend(updateLocale)
dayjs.updateLocale('en', {
/**/
})
Day.js 的语言对象模版
const localeObject = {
name: 'es', // name String
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
weekStart: 1, // OPTIONAL, set the start of a week. If the value is 1, Monday will be the start of week instead of Sunday。
yearStart: 4, // OPTIONAL, the week that contains Jan 4th is the first week of the year.
months: 'Enero_Febrero ... '.split('_'), // months Array
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
formats: {
// abbreviated format options allowing localization
LTS: 'h:mm:ss A',
LT: 'h:mm A',
L: 'MM/DD/YYYY',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY h:mm A',
LLLL: 'dddd, MMMM D, YYYY h:mm A',
// lowercase/short, optional formats for localization
l: 'D/M/YYYY',
ll: 'D MMM, YYYY',
lll: 'D MMM, YYYY h:mm A',
llll: 'ddd, MMM D, YYYY h:mm A'
},
relativeTime: {
// relative time format strings, keep %s %d as the same
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours', // e.g. 2 hours, %d been replaced with 2
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// OPTIONAL, AM/PM
return hour > 12 ? 'PM' : 'AM'
}
}
Day.js 的语言文件模板。 (例 dayjs/locale/es.js)
import dayjs from 'dayjs'
const locale = { ... } // Day.js 的语言对象.
dayjs.locale(locale, null, true) // load locale for later use
export default locale
5587

被折叠的 条评论
为什么被折叠?



