1、今天
[moment(), moment()]
2、明天
moment(new Date()).add(1,'days');
3、昨天
moment(new Date()).add(-1, 'days');
4、本周
[moment().startOf('week'), moment().endOf('week')]
5、上周
[moment().week(moment().week() - 1).startOf('week'), moment().week(moment().week() - 1).endOf('week')]
6、本月
[moment().startOf('month'), moment().endOf('month')]
7、上月
[moment().month(moment().month() - 1).startOf('month'), moment().month(moment().month() - 1).endOf('month')]
8、今年
[moment().startOf('year'), moment().endOf('year')]
9、去年
[moment().year(moment().year() - 1).startOf('year'), moment().year(moment().year() - 1).endOf('year')]
10、获取今天开始(0时0分0秒)、结束(23时59分59秒)
moment().startOf('day');
moment().endOf('day');
11、获取本周周一开始(0时0分0秒)、结束月(23时59分59秒)
moment().startOf('isoWeek');
moment().endOf('isoWeek');
12、获取当前月第一天开始(0时0分0秒)、结束天(23时59分59秒)
moment().startOf('month');
moment().endOf('month');
13、获取时间戳
moment().format('X') // 返回值为字符串类型
moment().unix() // 返回值为数值型
moment().format('x') // 返回值为字符串类型
moment().valueOf() // 返回值为数值型
14、时间获取
moment().year()
moment().get('year')
moment().month() (0~11, 0: January, 11: December)
moment().get('month')
moment().date()
moment().get('date')
moment().hours()
moment().get('hours')
moment().minutes()
moment().get('minutes')
moment().seconds()
moment().get('seconds')
moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
moment().toObject() // {years: xxxx, months: x, date: xx ...}
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')
15、设置时间
moment().year(2019)
moment().set('year', 2019)
moment().set({year: 2019})
moment().month(11) (0~11, 0: January, 11: December)
moment().set('month', 11)
moment().date(15)
moment().set('date', 15)
moment().weekday(0) // 设置日期为本周第一天(周日)
moment().isoWeekday(1) // 设置日期为本周周一
moment().set('weekday', 0)
moment().set('isoWeekday', 1)
moment().hours(12)
moment().set('hours', 12)
moment().minutes(30)
moment().set('minutes', 30)
moment().seconds(30)
moment().set('seconds', 30)
16、时间加减
moment().add(1, 'years')
moment().add({years: 1})
moment().subtract(1, 'years')
moment().subtract({years: 1})
moment().add(1, 'months')
moment().subtract(1, 'months')
moment().add(1, 'days')
moment().subtract(1, 'days')
moment().add(1, 'weeks')
moment().subtract(1, 'weeks')
moment().add(1, 'hours')
moment().subtract(1, 'hours')
moment().add(1, 'minutes')
moment().subtract(1, 'minutes')
moment().add(1, 'seconds')
moment().subtract(1, 'seconds')
17、时间格式化
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') // 返回值为字符串类型