Moment.js是一個輕量級的JavaScript時間庫,它方便了日常開發(fā)中對時間的操作,提高了開發(fā)效率。
日常開發(fā)中,通常會對時間進行下面這幾個操作:比如獲取時間,設(shè)置時間,格式化時間,比較時間等等。接下來,我將按照這些操作對Moment.js中的Doc進行整理分類,方便學(xué)習(xí)和日后的查閱。
moment.js是一款轉(zhuǎn)換時間的插件
安裝方式為:
npm install moment --save
其常用的格式化參數(shù)如下表所示:
格式代碼 說明 返回值例子
M 數(shù)字表示的月份,沒有前導(dǎo)零 1到12
MM 數(shù)字表示的月份,有前導(dǎo)零 01到12
MMM 三個字母縮寫表示的月份 Jan到Dec
MMMM 月份,完整的文本格式 January到December
Q 季度 1到4
D 月份中的第幾天,沒有前導(dǎo)零 1到31
DD 月份中的第幾天,有前導(dǎo)零 01到31
d 星期中的第幾天,數(shù)字表示 0到6,0表示周日,6表示周六
ddd 三個字母表示星期中的第幾天 Sun到Sat
dddd 星期幾,完整的星期文本 從Sunday到Saturday
w 年份中的第幾周 如42:表示第42周
YYYY 四位數(shù)字完整表示的年份 如:2014 或 2000
YY 兩位數(shù)字表示的年份 如:14 或 98
A 大寫的AM PM AM PM
a 小寫的am pm am pm
HH 小時,24小時制,有前導(dǎo)零 00到23
H 小時,24小時制,無前導(dǎo)零 0到23
hh 小時,12小時制,有前導(dǎo)零 00到12
h 小時,12小時制,無前導(dǎo)零 0到12
m 沒有前導(dǎo)零的分鐘數(shù) 0到59
mm 有前導(dǎo)零的分鐘數(shù) 00到59
s 沒有前導(dǎo)零的秒數(shù) 1到59
ss 有前導(dǎo)零的描述 01到59
X Unix時間戳 1411572969
常用的轉(zhuǎn)換
1.時間格式化
moment().format('YYYY-MM-DD HH:mm:ss');獲取當(dāng)前時間 2018-09-18 12:06:09
moment().format('L') 2018-07-04
moment().format('LL') 2018年7月4日
moment().format('LLL') 2018年7月4日晚上5點55分
moment().format('LLLL') 2018年7月4日星期三晚上5點55分
moment().format('X'); 轉(zhuǎn)換當(dāng)前時間的Unix時間戳
moment().format('d'); 今天是周幾
2.日期格式化
moment().format('dddd') 星期三
moment().format('MMM Do YY') 7月4日 18
moment().format() 2018-07-04T18:04:52+08:00
3.日歷格式化
moment().subtract(1,'days').calendar();昨天下午5點55分
moment.calendar();今天下午5點55分
moment().add(1,'days').calendar();明天下午5點55分
4.時間戳轉(zhuǎn)為時間
(1)時間戳秒轉(zhuǎn)時間
moment(Number);
(2)時間戳毫秒轉(zhuǎn)時間
var day = moment.unix(1318781876);
5.時間差轉(zhuǎn)化為相對的秒、分鐘或者小時
這里的stopTime和 startTime為開始和結(jié)束的日期格式,需要將時間差轉(zhuǎn)化為秒或者分鐘,再進行邏輯判斷。
let consumingSeconds = moment.duration(moment(stopTime).valueOf()- moment(startTime).valueOf()).as('seconds');
let consumingMinutes = moment.duration(moment(stopTime).valueOf()- moment(startTime).valueOf()).as('minutes');
let consumingHours = moment.duration(moment(stopTime).valueOf()- moment(startTime).valueOf()).as('hours');
獲取時間
Start of Time
moment().startOf(String)
獲取今天0時0分0秒
moment().startOf('day')
獲取本周第一天(周日)0時0分0秒
moment().startOf('week')
獲取本周周一0時0分0秒
moment().startOf('isoWeek')
獲取當(dāng)前月第一天0時0分0秒
moment().startOf('month')
End of Time
moment().endOf(String)
獲取今天23時59分59秒
moment().endOf('day')
獲取本周最后一天(周六)23時59分59秒
moment().endOf('week')
獲取本周周日23時59分59秒
moment().endOf('isoWeek')
獲取當(dāng)前月最后一天23時59分59秒
moment().endOf('month')
Days in Month
moment().daysInMonth()
獲取當(dāng)前月的總天數(shù)
moment().daysInMonth()
Timestamp
獲取時間戳(以秒為單位)
moment().format('X') // 返回值為字符串類型
moment().unix() // 返回值為數(shù)值型
獲取時間戳(以毫秒為單位)
moment().format('x') // 返回值為字符串類型
moment().valueOf() // 返回值為數(shù)值型
Get Time
獲取年份
moment().year()
moment().get('year')
獲取月份
moment().month() (0~11, 0: January, 11: December)
moment().get('month')
獲取一個月中的某一天
moment().date() moment().get('date')
獲取一個星期中的某一天
moment().day() (0~6, 0: Sunday, 6: Saturday)
moment().weekday() (0~6, 0: Sunday, 6: Saturday)
moment().isoWeekday() (1~7, 1: Monday, 7: Sunday)
moment().get('day')
mment().get('weekday')
moment().get('isoWeekday')
獲取小時
moment().hours() moment().get('hours')
獲取分鐘
moment().minutes() moment().get('minutes')
獲取秒數(shù)
moment().seconds() moment().get('seconds')
獲取當(dāng)前的年月日時分秒
moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
moment().toObject() // {years: xxxx, months: x, date: xx ...}
設(shè)置時間
Set Time
moment().year(Number), moment().month(Number)...
moment().set(String, Int)
moment().set(Object)
設(shè)置年份
moment().year(2019)
moment().set('year', 2019)
moment().set({year: 2019})
設(shè)置月份
moment().month(11) (0~11, 0: January, 11: December)
moment().set('month', 11)
設(shè)置某個月中的某一天
moment().date(15)
moment().set('date', 15)
設(shè)置某個星期中的某一天
moment().weekday(0) // 設(shè)置日期為本周第一天(周日)
moment().isoWeekday(1) // 設(shè)置日期為本周周一
moment().set('weekday', 0)
moment().set('isoWeekday', 1)
設(shè)置小時
moment().hours(12)
moment().set('hours', 12)
設(shè)置分鐘
moment().minutes(30)
moment().set('minutes', 30)
設(shè)置秒數(shù)
moment().seconds(30)
moment().set('seconds', 30)
Add Time
moment().add(Number, String)
moment().add(Object)
設(shè)置年份
moment().add(1, 'years')
moment().add({years: 1})
設(shè)置月份
moment().add(1, 'months')
設(shè)置日期
moment().add(1, 'days')
設(shè)置星期
moment().add(1, 'weeks')
設(shè)置小時
moment().add(1, 'hours')
設(shè)置分鐘
moment().add(1, 'minutes')
設(shè)置秒數(shù)
moment().add(1, 'seconds')
Subtract Time
moment().subtract(Number, String)
moment().subtract(Object)
設(shè)置年份
moment().subtract(1, 'years')
moment().subtract({years: 1})
設(shè)置月份
moment().subtract(1, 'months')
設(shè)置日期
moment().subtract(1, 'days')
設(shè)置星期
moment().subtract(1, 'weeks')
設(shè)置小時
moment().subtract(1, 'hours')
設(shè)置分鐘
moment().subtract(1, 'minutes')
設(shè)置秒數(shù)
moment().subtract(1, 'seconds')
格式化時間
Format Time
moment().format()
moment().format(String)
格式化年月日: 'xxxx年xx月xx日'
moment().format('YYYY年MM月DD日')
格式化年月日: 'xxxx-xx-xx'
moment().format('YYYY-MM-DD')
格式化時分秒(24小時制): 'xx時xx分xx秒'
moment().format('HH時mm分ss秒')
格式化時分秒(12小時制):'xx:xx:xx am/pm'
moment().format('hh:mm:ss a')
格式化時間戳(以秒為單位)
moment().format('X') // 返回值為字符串類型
格式化時間戳(以毫秒為單位)
moment().format('x') // 返回值為字符串類型
比較時間
Difference
moment().diff(Moment|String|Number|Date|Array)
獲取兩個日期之間的時間差
let start_date = moment().subtract(1, 'weeks')
let end_date = moment()
end_date.diff(start_date) // 返回毫秒數(shù)
end_date.diff(start_date, 'months') // 0
end_date.diff(start_date, 'weeks') // 1
end_date.diff(start_date, 'days') // 7
start_date.diff(end_date, 'days') // -7
轉(zhuǎn)化為JavaScript原生Date對象
moment().toDate()
new Date(moment())
將Moment時間轉(zhuǎn)換為JavaScript原生Date對象
let m = moment()
let nativeDate1 = m.toDate()
let nativeDate2 = new Date(m)
String(nativeDate1) === String(nativeDate2) // true