Moment.js 常用操作
2022-01-20 本文已影响0人
1baibai
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、时间格式化
- 格式化年月日: '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') // 返回值为字符串类型