JavaScript 时间处理库之 Day.js 的常用 API

dayjs中文官网
Day.js: Moment.js 的 2kB 轻量化方案,拥有同样强大的 API

解析

// 解析 param 返回一个day.js 对象,如下图:
dayjs()
// 使用 .format()  可转成字符串,否则返回的都是  dayjs 对象

// "2018-04-05T00:00:00+08:00"
dayjs('2018-04-04T16:00:00.000Z').format()

// "1995-12-25T00:00:00+08:00"
dayjs("12-25-1995", "MM-DD-YYYY").format()

// 多种可能格式可以使用数组
dayjs("12-25-2001", ["YYYY", "YYYY-MM-DD"], 'es', true).format()

// 支持传入 秒 和 毫秒
dayjs(1318781876406)

// 传入 Date 对象
dayjs(new Date())

// UTC 时间(默认是本地时间)
dayjs.utc().format()

dayjs()

输入 例子 详情
YY 01 两位数的年份
YYYY 2001 四位数的年份
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 毫秒,三位数
Z -05:00 UTC 的偏移量
ZZ -0500 UTC 的偏移量,两位数
A AM PM 上午 下午 大写
a am pm 上午 下午 小写
Do 1st… 31st 带序数词的月份里的一天
X 1410715640.579 Unix 时间戳
x 1410715640579 Unix 时间戳

取值/赋值

// 设置&获取 dayjs对象 秒数
dayjs().second(30)
// 获取当前秒数  30
dayjs().second()
eg:dayjs(dayjs().second(32)).second() => 32

// 设置&获取毫秒数 0~999,超过会进位到秒
dayjs().millisecond()
dayjs().millisecond(1)

// 设置&获取秒数(0~59)
dayjs().second()
dayjs().second(1)

// 设置&获取分钟(0~59)
dayjs().minute()
dayjs().minute(59)

// 设置&获取小时(0~23)
dayjs().hour()
dayjs().hour(12)

// 设置&获取日期(1~31)
dayjs().date()
dayjs().date(1)

// 设置&获取星期(0~6,其中 0 是周日)
dayjs().day()
dayjs().day(0)

// 设置&获取年份里的第几天(1~366)
dayjs.extend(dayOfYear)
dayjs('2010-01-01').dayOfYear() // 1
dayjs('2010-01-01').dayOfYear(365) // 2010-12-31

// 获取或设置该年的第几周
dayjs.extend(weekOfYear)
dayjs('2018-07-27').week() // 26
dayjs('2018-06-27').week(5) // 设置周

// 获取或设置月份(0~11)
dayjs().month()
dayjs().month(0)

// 获取或设置季度
dayjs.extend(quarterOfYear)
dayjs('2010-04-01').quarter() // 2
dayjs('2010-04-01').quarter(2)

// 获取或设置年份
dayjs().year()
dayjs().year(2000)

// 从 Day.js 对象中获取相应信息的 getter
// 各个传入的单位对大小写不敏感,支持缩写和复数。 请注意,缩写是区分大小写的。
dayjs().get('year')
dayjs().get('month') // start 0
dayjs().get('date')
dayjs().get('hour')
dayjs().get('minute')
dayjs().get('second')
dayjs().get('millisecond')

// 通用的 setter,两个参数分别是要更新的单位和数值,调用后会返回一个修改后的新实例
dayjs().set('date', 1)
dayjs().set('month', 3) // 四月
dayjs().set('second', 30)

// 返回传入的 Day.js 实例中的最大的 (即最靠近未来的)。 它接受传入多个 Day.js实例或一个数组
dayjs.extend(minMax)
dayjs.max(dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01'))
dayjs.max([dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01')])

// 返回传入的 Day.js 实例中的最小的 (即最靠近过去的)。 它接受传入多个 Day.js实例或一个数组
dayjs.extend(minMax)
dayjs.min(dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01'))
dayjs.min([dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01')])
单位 缩写 详情
date D 月份里的日期
day d 星期几 (星期天0,星期六6)
month M 月份 (一月 0, 十二月 11)
year y 年份
hour h 小时
minute m 分钟
second s
millisecond ms 毫秒

操作

// Day.js 支持像这样的链式调用
dayjs('2019-01-25').add(1, 'day').subtract(1, 'year').year(2009).toString()

// 返回复制的 Day.js 对象,并设置到一个时间的开始
dayjs().startOf('year')

// 返回复制的 Day.js 对象,并设置到一个时间的末尾
dayjs().endOf('month')
单位 缩写 详情
year y 今年一月1日上午 00:00
quarter Q 本季度第一个月1日上午 00:00 ( 依赖 QuarterOfYear 插件 )
month M 本月1日上午 00:00
week w 本周的第一天上午 00:00
isoWeek 本周的第一天上午 00:00 (根据 ISO 8601) ( 依赖 IsoWeek 插件 )
date D 当天 00:00
day d 当天 00:00
hour h 当前时间,0 分、0 秒、0 毫秒
minute m 当前时间,0 秒、0 毫秒
second s 当前时间,0 毫秒

显示(重要)

// 根据传入的占位符返回格式化后的日期
// 将字符放在方括号中,即可原样返回而不被格式化替换 (例如, [MM])。
dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'

// 返回现在到当前实例的相对时间
dayjs.extend(relativeTime)
dayjs('1999-01-01').fromNow() // 22 年前

// 返回 X 到当前实例的相对时间
// 如果传入 true,则可以获得不带后缀的值(1年)
dayjs.extend(relativeTime)
let a = dayjs('2000-01-01')
dayjs('1999-01-01').from(a) // 1 年前

// 返回当前实例到现在的相对时间
dayjs.extend(relativeTime)
dayjs('1999-01-01').toNow() // 22 年后

// 返回当前实例到 X 的相对时间
dayjs.extend(relativeTime)
var a = dayjs('2000-01-01')
dayjs('1999-01-01').to(a) // 1 年后

// 日历时间显示了距离给定时间 (默认为现在) 的相对时间,但与 dayjs#fromnow 略有不同
dayjs.extend(calendar)
dayjs().calendar()
dayjs().calendar(dayjs('2008-01-01'))

// 返回指定单位下两个日期时间之间的差异
const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000 默认单位是毫秒
// 要获取其他单位下的差异,则在第二个参数传入相应的单位。
const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'month') // 7

// 返回当前实例的 UNIX 时间戳,13位数字,毫秒
dayjs('2019-01-25').valueOf() // 1548381600000
+dayjs(1548381600000) // 1548381600000
// 返回当前实例的 UNIX 时间戳,10位数字,秒。
dayjs('2019-01-25').unix() // 1548381600
// 获取当前月份包含的天数
dayjs('2019-01-25').daysInMonth() // 31
// 调用 dayjs#toDate 从 Day.js 对象中获取原生的 Date 对象
dayjs('2019-01-25').toDate()
占位符 输出 详情
YY 18 两位数的年份
YYYY 2018 四位数的年份
M 1-12 月份,从 1 开始
MM 01-12 月份,两位数
MMM Jan-Dec 缩写的月份名称
MMMM January-December 完整的月份名称
D 1-31 月份里的一天
DD 01-31 月份里的一天,两位数
d 0-6 一周中的一天,星期天是 0
dd Su-Sa 最简写的星期几
ddd Sun-Sat 简写的星期几
dddd Sunday-Saturday 星期几
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 秒 两位数
SSS 000-999 毫秒 三位数
Z +05:00 UTC 的偏移量,±HH:mm
ZZ +0500 UTC 的偏移量,±HHmm
A AM PM
a am pm
其他格式 ( 依赖 AdvancedFormat 插件 )
范围 键值 示例输出
0 到 44 秒 s 几秒前
45 到 89 秒 m 1 分钟前
90 秒 到 44 分 mm 2 分钟前 … 44 分钟前
45 到 89 分 h 1 小时前
90 分 到 21 小时 hh 2 小时前 … 21 小时前
22 到 35 小时 d 1 天前
36 小时 到 25 天 dd 2 天前 … 25 天前
26 到 45 天 M 1 个月前
46 天 到 10 月 MM 2 个月前 … 10 个月前
11 月 到 17 月 y 1 年前
18 月以上 yy 2 年前 … 20 年前

查询

// 这表示 Day.js 对象是否在另一个提供的日期时间之前
dayjs().isBefore(dayjs('2011-01-01')) // 默认毫秒
// 如果想使用除了毫秒以外的单位进行比较,则将单位作为第二个参数传入
dayjs().isBefore('2011-01-01', 'year')

// 其他
dayjs().isSame('2011-01-01', 'year')
dayjs().isAfter('2011-01-01', 'year')

dayjs.extend(isBetween)
dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25')) 
// 默认毫秒

插件

var AdvancedFormat = require('dayjs/plugin/advancedFormat')
// import AdvancedFormat from 'dayjs/plugin/advancedFormat' // ES 2015

dayjs.extend(AdvancedFormat) // use plugin

猜你喜欢

转载自blog.csdn.net/weixin_44778151/article/details/122010356