Introduction
Day.js is a lightweight JavaScript library that handles time and date, which is exactly the same as the API design of Moment.js. If you have used Moment.js before, then you already know how to use Day.js
installation
npm install dayjs --save
import dayjs from 'dayjs'
// 或者 CommonJS
// var dayjs = require('dayjs');
dayjs().format()
API
Dayjs
Javascript does not change or override native Date.prototype
, but to create a new containing Javascript Date
objects Dayjs
objects.
Dayjs
Objects are immutable, all the operations of the API will return a new Dayjs
object.
Unless otherwise specified, Day.js return value is a new Dayjs
object.
Parsing
In dayjs()
passing the supported formats
current time
Run directly dayjs()
to obtain contains the current time and date Dayjs
objects.
dayjs()
Time string
It can parse a standard ISO 8601 time string passed in .
dayjs(String)
dayjs('1995-12-25')
Date object
A Javascript Date object passed in can be parsed.
dayjs(Date)
dayjs(new Date(2018, 8, 18))
Unix timestamp (milliseconds)
It can parse the incoming Unix timestamp (13 digits).
dayjs(Number)
dayjs(1318781876406)
Unix timestamp (seconds)
It can parse the incoming Unix timestamp (10 digits).
dayjs.unix(Number)
dayjs.unix(1318781876)
Custom time format
- Parsing custom time formats such as
dayjs("12-25-1995", "MM-DD-YYYY")
may be used widgetCustomParseFormat
copy
Dayjs
Objects are immutable. If you want to get a copy of an object, please do .clone()
.
To dayjs()
pass in a Dayjs
subject can achieve the same effect.
dayjs(Dayjs)
dayjs().clone()
verification
- return Boolean
The current detection Dayjs
whether the object is a valid time.
dayjs().isValid()
Get + set
Get and change the date.
year
Get or set the year.
dayjs().year()
dayjs().year(2000)
month
Get or set the month. Start from 0
dayjs().month()
dayjs().month(0)
day
Get or set the date. Start from 1
dayjs().date()
dayjs().date(1)
week
Get or set the day of the week. Start on Sunday 0
dayjs().day()
dayjs().day(0)
Time
Get or set the hour.
dayjs().hour()
dayjs().hour(12)
Minute
Get or set the minutes.
dayjs().minute()
dayjs().minute(59)
second
Get or set seconds.
dayjs().second()
dayjs().second(1)
millisecond
Get or set milliseconds.
dayjs().millisecond()
dayjs().millisecond(1)
Obtain
Acquired from Dayjs
the information object to take
an incoming unit (unit) not case-sensitive.
dayjs().get(unit : String)
dayjs().get('month') // 从 0 开始
dayjs().get('day')
Available units
unit | abbreviation | description |
---|---|---|
date |
date | |
day |
d |
Day of the week (Sunday 0, Saturday 6) |
month |
M |
Month (January 0, December 11) |
year |
y |
year |
hour |
h |
Time |
minute |
m |
Minute |
second |
s |
second |
millisecond |
ms |
millisecond |
Set up
Set time
dayjs().set(unit : String, value : Int);
dayjs().set('date', 1);
dayjs().set('month', 3); // 四月
dayjs().set('second', 30);
operating
You can Dayjs
target the following operations increased to reduce the class:
dayjs()
.startOf('month')
.add(1, 'day')
.subtract(1, 'year')
increase
Increase the time and returns a new Dayjs()
object.
dayjs().add(value : Number, unit : String);
dayjs().add(7, 'day');
cut back
Reduce the time and returns a new Dayjs()
object.
dayjs().subtract(value : Number, unit : String);
dayjs().subtract(7, 'year');
Start time
Returns the current time of the beginning of the time of Dayjs()
an object, such as the first day of the month.
dayjs().startOf(unit : String);
dayjs().startOf('week'); // 取决于 locale 文件里 `weekStart` 的值
End time
Returns the current time to the end time of Dayjs()
the object, such as the last day of the month.
dayjs().endOf(unit : String);
dayjs().endOf('month');
display
Formatting Dayjs
objects and display.
format
- return String
Receive a series of time and date strings and replace them with corresponding values.
dayjs().format(String)
dayjs('2019-01-25').format('[YYYY] YYYY-MM-DDTHH:mm:ssZ[Z]') // 'YYYY 2019-01-25T00:00:00-02:00Z'
dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]') // "{2014} 09-08T08:02:17-05:00Z"
Details are as follows:
format | Output | description |
---|---|---|
YY |
18 | Two-digit year |
YYYY |
2018 | Four-digit year |
M |
1-12 | Month, starting from 1 |
MM |
01-12 | Month, two digits |
MMM |
Jan-Dec | Abbreviated month name |
MMMM |
January-December | Full month name |
D |
1-31 | Day of the month |
DD |
01-31 | Day of the month, two digits |
d |
0-6 | Day of the week, Sunday is 0 |
dd |
Drought | The shortest name of the day of the week |
ddd |
Sun-Sat | Abbreviated name of the day of the week |
dddd |
Sunday-Saturday | The name of the day of the week |
H |
0-23 | hour |
HH |
00-23 | Hour, two digits |
h |
1-12 | Hour, 12 hour clock |
hh |
01-12 | Hours, 12-hour clock, two digits |
m |
0-59 | minute |
mm |
00-59 | Minutes, two digits |
s |
0-59 | second |
ss |
00-59 | Two digits for seconds |
SSS |
000-999 | Three digits in milliseconds |
Z |
+5:00 | UTC offset |
ZZ |
+0500 | UTC offset, prefix the number with 0 |
A |
AM PM | |
a |
am pm |
- More information about formatting options
Q Do k kk X x ...
you can use plug-insAdvancedFormat
- 本地化的长日期格式
L LT LTS ...
可以使用插件LocalizedFormat
时间差
- return Number
获取两个 Dayjs
对象的时间差,默认毫秒。
const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000
date1.diff(date2, 'month') // 7
date1.diff(date2, 'month', true) // 7.645161290322581
date1.diff(date2, 'day') // 233
Unix 时间戳 (毫秒)
- return Number
返回 Unix 时间戳 (毫秒)
dayjs().valueOf()
Unix 时间戳 (秒)
- return Number
返回 Unix 时间戳 (秒)。
dayjs().unix()
UTC 偏移量 (分)
返回 UTC 偏移量 (分)
dayjs().utcOffset()
天数 (月)
- return Number
返回月份的天数。
dayjs().daysInMonth()
Date 对象
- return Javascript
Date
object
返回原生的 Date
对象。
dayjs().toDate()
As JSON
- return JSON String
当序列化 Dayjs
对象时,会返回 ISO8601 格式的字符串。
dayjs().toJSON() //"2018-08-08T00:00:00.000Z"
ISO 8601 字符串
- return String
返回 ISO8601 格式的字符串。
dayjs().toISOString()
字符串
- return String
dayjs().toString()
查询
是否之前
- return Boolean
检查一个 Dayjs
对象是否在另一个 Dayjs
对象时间之前。
dayjs().isBefore(Dayjs, unit? : String);
dayjs().isBefore(dayjs()); // false
dayjs().isBefore(dayjs(), 'year'); // false
是否相同
- return Boolean
检查一个 Dayjs
对象是否和另一个 Dayjs
对象时间相同。
dayjs().isSame(Dayjs, unit? : String);
dayjs().isSame(dayjs()); // true
dayjs().isSame(dayjs(), 'year'); // true
是否之后
- return Boolean
检查一个 Dayjs
对象是否在另一个 Dayjs
对象时间之后。
dayjs().isAfter(Dayjs, unit? : String);
dayjs().isAfter(dayjs()); // false
dayjs().isAfter(dayjs(), 'year'); // false
是否是 Dayjs .isDayjs(compared: any)
返回一个 boolean
验证传入值是否是一个 Dayjs 对象.
dayjs.isDayjs(dayjs()) // true
dayjs.isDayjs(new Date()) // false
也可以使用 instanceof
dayjs() instanceof dayjs // true
UTC
如果想要使用 UTC 模式来解析和展示时间,.utc
.local
.isUTC
可以使用插件 UTC
插件 APIs
相对时间
.from
.to
.fromNow
.toNow
获得相对时间
插件 RelativeTime
是否是闰年
.isLeapYear
获得是否闰年
插件 IsLeapYear
年中的第几周
.week
获取是第几个周
插件 WeekOfYear
星期
.weekday
来获取或设置当前语言的星期
plugin WeekDay
年中有几周 ISO
.isoWeeksInYear
获得年中有几周
plugin IsoWeeksInYear
是否相同或之后
.isSameOrAfter
返回一个时间和一个时间相同或在一个时间之后
是否相同或之前
.isSameOrBefore
返回一个时间是否和一个时间相同或在一个时间之前
是否之间
.isBetween
返回一个时间是否介于两个时间之间
插件 IsBetween
年中第几季度
.quarter
返回年中第几季度
转成数组
.toArray
返回包含时间数值的数组。
插件 ToArray
转成对象
.toObject
返回包含时间数值的对象
插件 ToObject
最小最大
.min
.max
比较传入的 dayjs 实例的大小
plugin MinMax
日历时间
.calendar
来显示日历时间
plugin Calendar
更多最新内容参考:https://github.com/iamkun/dayjs/blob/master/docs/zh-cn/README.zh-CN.md