Moment.js常见用法归纳总结


 

Moment.js — JavaScript 日期处理类库

 

安装

1 npm install moment --save

 

配置使用

1 import moment from 'moment'
2 
3 moment().format();

 

格式化时间

 1 moment().format('YYYY年MM月DD日') // 格式化年月日: 'xxxx年xx月xx日'
 2 
 3 moment().format('YYYY-MM-DD') // 格式化年月日: 'xxxx-xx-xx'
 4 
 5 moment().format('HH时mm分ss秒') // 格式化时分秒(24小时制):'xx时xx分xx秒'
 6 
 7 moment().format('hh:mm:ss a') // 格式化时分秒(12小时制):'xx:xx:xx am/pm'
 8 
 9 moment().format('X') // 格式化时间戳(以秒为单位),返回值为字符串类型
10 
11 moment().format('x') // 格式化时间戳(以毫秒为单位),返回值为字符串类型

 

获取时间

 1 moment().startOf('day') // 获取今天0时0分0秒
 2 
 3 moment().startOf('week') // 获取本周第一天(周日)0时0分0秒
 4 
 5 moment().startOf('isoWeek') // 获取本周周一0时0分0秒
 6 
 7 moment().startOf('month') // 获取当前月第一天0时0分0秒
 8 
 9 moment().endOf('day') // 获取今天23时59分59秒
10 
11 moment().endOf('week') // 获取本周最后一天(周六)23时59分59秒
12 
13 moment().endOf('isoWeek') // 获取本周周日23时59分59秒
14 
15 moment().endOf('month') // 获取当前月最后一天23时59分59秒
16 
17 moment().daysInMonth() // 获取当前月的总天数
18 
19 moment().format('X') // 获取时间戳(以秒为单位),返回值为字符串类型
20 
21 moment().unix() // 获取时间戳(以秒为单位),返回值为数值型
22 
23 moment().format('x') // 获取时间戳(以毫秒为单位),返回值为字符串类型
24 
25 moment().valueOf() // 获取时间戳(以毫秒为单位),返回值为数值型
26 
27 // 获取年份
28 moment().year()
29 moment().get('year')
30 
31 // 获取月份
32 moment().month() (0~11, 0: January, 11: December)
33 moment().get('month')
34 
35 // 获取一个月中的某一天
36 moment().date()
37 moment().get('date')
38 
39 // 获取一个星期中的某一天
40 moment().day() (0~6, 0: Sunday, 6: Saturday)
41 moment().weekday() (0~6, 0: Sunday, 6: Saturday)
42 moment().isoWeekday() (1~7, 1: Monday, 7: Sunday)
43 moment().get('day')
44 mment().get('weekday')
45 moment().get('isoWeekday')
46 
47 // 获取小时
48 moment().hours()
49 moment().get('hours')
50 
51 // 获取分钟
52 moment().minutes()
53 moment().get('minutes')
54 
55 // 获取秒数
56 moment().seconds()
57 moment().get('seconds')
58 
59 // 获取当前的年月日时分秒
60 moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
61 moment().toObject() // {years: xxxx, months: x, date: xx ...}

 

设置时间

 1 // 设置年份
 2 moment().year(2019)
 3 moment().set('year', 2019)
 4 moment().set({year: 2019})
 5 moment().add(1, 'years')
 6 moment().add({years: 1})
 7 moment().subtract(1, 'years')
 8 moment().subtract({years: 1})
 9 
10 // 设置月份
11 moment().month(11) (0~11, 0: January, 11: December)
12 moment().set('month', 11)
13 moment().add(1, 'months')
14 moment().subtract(1, 'months')
15 
16 // 设置日期
17 moment().add(1, 'days')
18 moment().subtract(1, 'days')
19 
20 // 设置星期
21 moment().add(1, 'weeks')
22 moment().subtract(1, 'weeks')
23 
24 // 设置某个月中的某一天
25 moment().date(15)
26 moment().set('date', 15)
27 
28 // 设置某个星期中的某一天
29 moment().weekday(0) // 设置日期为本周第一天(周日)
30 moment().isoWeekday(1) // 设置日期为本周周一
31 moment().set('weekday', 0)
32 moment().set('isoWeekday', 1)
33 
34 // 设置小时
35 moment().hours(12)
36 moment().set('hours', 12)
37 moment().add(1, 'hours')
38 moment().subtract(1, 'hours')
39 
40 // 设置分钟
41 moment().minutes(30)
42 moment().set('minutes', 30)
43 moment().add(1, 'minutes')
44 moment().subtract(1, 'minutes')
45 
46 // 设置秒数
47 moment().seconds(30)
48 moment().set('seconds', 30)
49 moment().add(1, 'seconds')
50 moment().subtract(1, 'seconds')

 

参考文献

 

posted @ 2021-02-24 14:22  地yu荒冢  阅读(445)  评论(0编辑  收藏  举报