dayjs中文官网
Day.js: Moment.js 的 2kB 轻量化方案,拥有同样强大的 API
解析
dayjs()
dayjs('2018-04-04T16:00:00.000Z').format()
dayjs("12-25-1995", "MM-DD-YYYY").format()
dayjs("12-25-2001", ["YYYY", "YYYY-MM-DD"], 'es', true).format()
dayjs(1318781876406)
dayjs(new Date())
dayjs.utc().format()

输入 | 例子 | 详情 |
---|
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().second(30)
dayjs().second()
eg:dayjs(dayjs().second(32)).second() => 32
dayjs().millisecond()
dayjs().millisecond(1)
dayjs().second()
dayjs().second(1)
dayjs().minute()
dayjs().minute(59)
dayjs().hour()
dayjs().hour(12)
dayjs().date()
dayjs().date(1)
dayjs().day()
dayjs().day(0)
dayjs.extend(dayOfYear)
dayjs('2010-01-01').dayOfYear()
dayjs('2010-01-01').dayOfYear(365)
dayjs.extend(weekOfYear)
dayjs('2018-07-27').week()
dayjs('2018-06-27').week(5)
dayjs().month()
dayjs().month(0)
dayjs.extend(quarterOfYear)
dayjs('2010-04-01').quarter()
dayjs('2010-04-01').quarter(2)
dayjs().year()
dayjs().year(2000)
dayjs().get('year')
dayjs().get('month')
dayjs().get('date')
dayjs().get('hour')
dayjs().get('minute')
dayjs().get('second')
dayjs().get('millisecond')
dayjs().set('date', 1)
dayjs().set('month', 3)
dayjs().set('second', 30)
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')])
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 | 毫秒 |
操作
dayjs('2019-01-25').add(1, 'day').subtract(1, 'year').year(2009).toString()
dayjs().startOf('year')
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 毫秒 |
显示(重要)
dayjs('2019-01-25').format('DD/MM/YYYY')
dayjs.extend(relativeTime)
dayjs('1999-01-01').fromNow()
dayjs.extend(relativeTime)
let a = dayjs('2000-01-01')
dayjs('1999-01-01').from(a)
dayjs.extend(relativeTime)
dayjs('1999-01-01').toNow()
dayjs.extend(relativeTime)
var a = dayjs('2000-01-01')
dayjs('1999-01-01').to(a)
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)
const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'month')
dayjs('2019-01-25').valueOf()
+dayjs(1548381600000)
dayjs('2019-01-25').unix()
dayjs('2019-01-25').daysInMonth()
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 年前 |
查询
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')
dayjs.extend(AdvancedFormat)
所有评论(0)