當前時間
dayjs()
時間字符串
dayjs('2018-06-03')
時間戳
dayjs(1528361259484)
Date 對象
dayjs(new Date(2018,8,18))
復制
dayjs().clone()
檢測當前 Dayjs 對象是否是一個有效的時間
dayjs().isValid()
獲取
年 : dayjs().year()
月 : dayjs().month()
日 : dayjs().date()
星期 : dayjs().day()
時 : dayjs().hour()
分 : dayjs().minute()
秒 : dayjs().second()
毫秒 : dayjs().millisecond()
設置
dayjs().set('year',2017)
dayjs().set('month',9)
增加時間并返回一個新的 Dayjs() 對象
dayjs().add(7, 'day')
dayjs().add(7, 'year')
減少時間并返回一個新的 Dayjs() 對象
dayjs().subtract(7, 'year')
dayjs().subtract(7, 'month')
返回當前時間的開頭時間的 Dayjs() 對象,如月份的第一天。
dayjs().startOf('year')
dayjs().startOf('month')
返回當前時間的末尾時間的 Dayjs() 對象,如月份的最后一天。
dayjs().endOf('month')
dayjs().endOf('year')
格式化
dayjs().format()
dayjs().format('YYYY-MM-DD dddd HH:mm:ss.SSS A')
時間差
dayjs('2018-06-08').diff(dayjs('2017-06-01'),'years')
dayjs('2018-06-08').diff(dayjs('2017-06-01'),'day')
dayjs('2018-06-08').diff(dayjs('2017-06-01'),'hour')
Unix 時間戳 (毫秒)
dayjs().valueOf()
Unix 時間戳 (秒)
dayjs().unix()
返回月份的天數
dayjs().daysInMonth()
返回原生的 Date 對象
dayjs().toDate()
返回包含時間數值的數組
dayjs().toArray()
當序列化 Dayjs 對象時,會返回 ISO8601 格式的字符串
dayjs().toJSON() //2018-06-08T02:44:30.599Z
返回 ISO8601 格式的字符串
dayjs().toISOString() //2018-06-08T02:46:06.554Z
返回包含時間數值的對象
dayjs().toObject()
字符串
dayjs().toString()
檢查一個 Dayjs 對象是否在另一個 Dayjs 對象時間之前
dayjs('2018-06-01').isBefore(dayjs('2018-06-02'))
檢查一個 Dayjs 對象是否和另一個 Dayjs 對象時間相同
dayjs().isSame(dayjs())
檢查一個 Dayjs 對象是否在另一個 Dayjs 對象時間之后
dayjs().isAfter(dayjs())
FormatOutputDescription
YY18兩位數的年份
YYYY2018四位數的年份
M1-12月份,從 1 開始
MM01-12月份,兩位數
MMMJan-Dec簡寫的月份名稱
MMMMJanuary-December完整的月份名稱
D1-31月份里的一天
DD01-31月份里的一天,兩位數
d0-6一周中的一天,星期天是 0
ddSu-Sa最簡寫的一周中一天的名稱
dddSun-Sat簡寫的一周中一天的名稱
ddddSunday-Saturday一周中一天的名稱
H0-23小時
HH00-23小時,兩位數
m0-59分鐘
mm00-59分鐘,兩位數
s0-59秒
ss00-59秒 兩位數
SSS000-999秒 三位數
Z+5:00UTC 的偏移量
ZZ+0500UTC 的偏移量,數字前面加上 0
AAMPM
aampm
作者:cjrfan
鏈接:http://www.lxweimin.com/p/af775b9829f0
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。