# dayjs
[官網](https://dayjs.gitee.io/zh-CN/)
Day.js 是一個輕量的處理時間和日期的 JavaScript 庫,和 Moment.js 的 API 設計保持完全一樣. 如果您曾經用過 Moment.js, 那么您已經知道如何使用 Day.js
### format()方法
```js
dayjs().format("YYYY-MM-DD?hh:mm:ss?a") // 2021-04-13 01:48:00 pm
dayjs().format("YYYY-MM-DD?HH:mm:ss") // 2021-04-13 13:48:00
dayjs().format("YY-M-D?H:m:s") // 21-4-13 13:48:0
dayjs().format("YY-M-D?h:m:s?a") // 21-4-13 1:48:0 pm
```
| 占位符 | 輸出 | 詳情 |
| --- | --- | --- |
| `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 |
## 插件
```js
// 加載語言文件
require("dayjs/locale/zh-cn");
dayjs.locale('zh-cn') // 全局使用
dayjs().locale('zh-cn').format() // 當前實例使用
varcustomParseFormat?=?require("dayjs/plugin/customParseFormat");
dayjs.extend(customParseFormat);
varrelativeTime?=?require("dayjs/plugin/relativeTime");
dayjs.extend(relativeTime);
```
### Time from now
返回現在到當前實例的相對時間。
```js
dayjs.extend(relativeTime)
dayjs('1999-01-01').fromNow() // 22 年前
dayjs.extend(relativeTime)
dayjs('1999-01-01').fromNow(true) // 22 年
dayjs.extend(relativeTime)
var a = dayjs('2000-01-01')
dayjs('1999-01-01').from(a) // 1 年前
```
### Time to now
返回當前實例到現在的相對時間。
```js
dayjs.extend(relativeTime)
dayjs('1999-01-01').toNow() // 22 年后
dayjs.extend(relativeTime)
dayjs('1999-01-01').toNow(true) // 22 年
dayjs.extend(relativeTime)
var a = dayjs('2000-01-01')
dayjs('1999-01-01').to(a) // 1 年后
```
| 范圍 | 鍵值 | 示例輸出 |
| --- | --- | --- |
| 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 年前 |
### Days in Month
獲取當前月份包含的天數。
~~~js
dayjs('2019-01-25').daysInMonth() // 31
~~~
### As Object
返回包含時間信息的 Object。
```
dayjs.extend(toObject)
dayjs('2019-01-25').toObject()
/* {
years: 2019,
months: 0,
date: 25,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0
}
*/
```
### IsToday
IsToday 增加了`.isToday()`API 來判斷當前 Day.js 對象是否是今天。
~~~js
var isToday = require('dayjs/plugin/isToday')
dayjs.extend(isToday)
dayjs().isToday() // true
~~~
### IsTomorrow
IsTomorrow 增加了`.isTomorrow()`API 來判斷當前 Day.js 對象是否是明天。
~~~js
var isTomorrow = require('dayjs/plugin/isTomorrow')
dayjs.extend(isTomorrow)
dayjs().add(1, 'day').isTomorrow() // true
~~~
### IsYesterday
IsYesterday 增加了`.isYesterday()`API 來判斷當前 Day.js 對象是否是昨天。
~~~js
var isYesterday = require('dayjs/plugin/isYesterday')
dayjs.extend(isYesterday)
dayjs().add(-1, 'day').isYesterday() // true
~~~
### IsoWeeksInYear
IsoWeeksInYear 增加了`.isoWeeksInYear()`API 返回一個`number`來得到依據 ISO week 標準一年中有幾周
> 使用本功能需先配置[`IsLeapYear`](https://dayjs.gitee.io/docs/zh-CN/plugin/is-leap-year)插件,才能正常運行
~~~javascript
var isoWeeksInYear = require('dayjs/plugin/isoWeeksInYear')
var isLeapYear = require('dayjs/plugin/isLeapYear') // dependent on isLeapYear plugin
dayjs.extend(isoWeeksInYear)
dayjs.extend(isLeapYear)
dayjs('2004-01-01').isoWeeksInYear() // 53
dayjs('2005-01-01').isoWeeksInYear() // 52
~~~
### Add
返回**增加**一定時間的復制的 Day.js 對象。
~~~js
dayjs().add(7, 'day')
~~~
各個傳入的單位對大小寫不敏感,支持縮寫和復數。 請注意,縮寫是區分大小寫的。
支持的單位列表
| 單位 | 縮寫 | 詳情 |
| --- | --- | --- |
| `day` | `d` | 星期幾 (星期天0,星期六6) |
| `week` | `w` | Week of Year |
| `month` | `M` | 月份 (一月 0, 十二月 11) |
| `quarter` | `Q` | 季度 ( 依賴[`QuarterOfYear`](https://dayjs.gitee.io/docs/zh-CN/plugin/quarter-of-year)插件 ) |
| `year` | `y` | Year |
| `hour` | `h` | Hour |
| `minute` | `m` | Minute |
| `second` | `s` | Second |
| `millisecond` | `ms` | Millisecond |
或者,也可以給 Day.js 對象增加一個[持續時間](https://dayjs.gitee.io/docs/zh-CN/durations/durations)。
~~~js
dayjs().add(dayjs.duration({'days' : 1}))
~~~
### Subtract (用法通Add)
返回**減去**一定時間的復制的 Day.js 對象。
~~~js
dayjs().subtract(7, 'year')
~~~
各個傳入的單位對大小寫不敏感,支持縮寫和復數。