Moment
- 參考
- 常用的方法
- 1、moment()
- 2、獲取get
- 3、格式format
- 4、設定subtract
- 5、開始startOf()
- 6、結束endOf()
- 7、總天數Days in Month
- 8、時間措
- 9、關于ant選擇時間的實戰
Moment.js是一個輕量級的js時間處理類別庫,其使用簡單,方便了日常開發中對時間的操作,提高了開發效率,
參考
npm install moment
常用的方法
1、moment()
- 獲取當前的日期和時間
moment()
- 獲取String的日期和時間
moment(String)
2、獲取get
- 獲取當天的年份
moment().get('year')
- 獲取當天的月份 0-11
moment().get('month')
- 獲取當天的日期
moment().get('date')
3、格式format
- 得到的時間格式為YYYY-MM-DD
moment(String,'YYYY-MM-DD')
moment(String).format('YYYY-MM-DD')
4、設定subtract
.subtract(Number, String);
- 設定年份
moment().subtract(1, 'years')
- 設定月份
moment().subtract(1, 'months')
- 設定日期
moment().subtract(1, 'days')
5、開始startOf()
通過將原始的 moment 設定為時間單位的開頭來對其進行更改,
.startOf(String);
- 獲取今天的0時0分0秒
moment().startOf('day')
- 獲取本周第一天的0時0分0秒
moment().startOf('week')
6、結束endOf()
通過將原始的 moment 設定為時間單位的末尾來對其進行更改
.endOf(String);
- 獲取今天的23時59分59秒
moment().endOf('day')
- 獲取本周第一天的23時59分59秒
moment().endOf('week')
7、總天數Days in Month
.daysInMonth()
- 獲取2月的天數,
moment("2012-02", "YYYY-MM").daysInMonth() // 29
8、時間措
.unix() //秒數
.valueOf() //毫秒數
- 獲取時間戳(以秒為單位)
moment().format('X').unix() // 回傳值為數值型
- 獲取時間戳(以毫秒為單位)
moment().format('x').valueOf() // 回傳值為數值型
9、關于ant選擇時間的實戰
在ant的a-range-picker組件的disabledDate使用

- 不能選擇今天之前的日期(包括今天)
disabledDate(current) {
return current && current < moment().endOf('day');
},
- 不能選擇今天之前的日期(不包括今天)
disabledDate(current) {
return current && current < moment().subtract(1, 'days').endOf('day')
},
- 點擊選擇的2019-01-01之前的資料無法確認
disabledDate(current) {
return current && current < moment('2019-01-01')
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/297159.html
標籤:其他
