moment.js使用超详细教程
作者:哇伊卡
Moment.js是一个轻量级的JavaScript时间库,它方便了日常开发中对时间的操作,提高了开发效率。日常开发中,通常会对时间进行下面这几个操作:比如获取时间,设置时间,格式化时间,比较时间等等。
一、引入moment.js
1.Node.js方式引入
(1)安装
● npm install moment
● yarn add moment
(2)引入
● require 方式
require ('moment/locale/zh-cn') moment.locale('zh-cn');● import 方式 import moment from'moment';
2. 浏览器方式引入
二、 设定moment区域为中国
// require 方式
require ('moment/locale/zh-cn') moment.locale('zh-cn');// import 方式
import 'moment/locale/zh-cn'moment.locale('zh-cn');三、使用
1.获取时间
(1)获取当前时间
moment()
(2)获取今天0时0分0秒
moment().startOf('day')(3)获取本周第一天(周日)0时0分0秒
moment().startOf('week')(4)获取本周周一0时0分0秒
moment().startOf('isoWeek')(5)获取当前月第一天0时0分0秒
moment().startOf('month')(6)获取今天23时59分59秒
moment().endOf('day')(7)获取本周最后一天(周六)23时59分59秒
moment().endOf('week')(8)获取本周周日23时59分59秒
moment().endOf('isoWeek')(9)获取当前月最后一天23时59分59秒
moment().endOf('month')(10)获取当前月的总天数
moment().daysInMonth()
(11)获取时间戳(以秒为单位)
moment().format('X') // 返回值为字符串类型
moment().unix() // 返回值为数值型(12)获取时间戳(以毫秒为单位)
moment().format('x') // 返回值为字符串类型
moment().valueOf() // 返回值为数值型(13)获取年份
moment().year()moment().get('year')(14)获取月份
moment().month() // (0\~11, 0: January, 11: December)
moment().get('month')(15)获取一个月中的某一天
moment().date()moment().get('date')(16)获取一个星期中的某一天
moment().day() // (06, 0: Sunday, 6: Saturday)
moment().weekday() // (06, 0: Sunday, 6: Saturday)
moment().isoWeekday() // (1\~7, 1: Monday, 7: Sunday)
moment().get('day')
moment().get('weekday')
moment().get('isoWeekday')(17)获取小时
moment().hours()
moment().get('hours')(18)获取分钟
moment().minutes()
moment().get('minutes')(19)获取秒数
moment().seconds()
moment().get('seconds')(20)获取当前的年月日时分秒
moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
moment().toObject() // {years: xxxx, months: x, date: xx ...}2. 设置时间
(1)设置年份
moment().year(2019)
moment().set('year',2019)
moment().set({year:2019})(2)设置月份
moment().month(11) // (0\~11, 0: January, 11: December)
moment().set('month',11)(3)设置某个月中的某一天
moment().date(15)
moment().set('date',15)(4)设置某个星期中的某一天
moment().weekday(0) // 设置日期为本周第一天(周日)
moment().isoWeekday(1) // 设置日期为本周周一
moment().set('weekday',0)
moment().set('isoWeekday',1)(5)设置小时
moment().hours(12)
moment().set('hours',12)(6)设置分钟
moment().minutes(30)
moment().set('minutes',30)(7)设置秒数
moment().seconds(30)
moment().set('seconds',30)(8)年份+1
moment().add(1,'years')
moment().add({years:1})(9)月份+1
moment().add(1,'months')
(10)日期+1
moment().add(1,'days')
(11)星期+1
moment().add(1,'weeks')
(12)小时+1
moment().add(1,'hours')
(13)分钟+1
moment().add(1,'minutes')
(14)秒数+1
moment().add(1,'seconds')
(15)年份-1
moment().subtract(1, 'years')
moment().subtract({years: 1})(16)月份-1
moment().subtract(1,'months')
(17)日期-1
moment().subtract(1,'days')
(18)星期-1
moment().subtract(1,'weeks')
(19)小时-1
moment().subtract(1,'hours')
(20)分钟-1
moment().subtract(1,'minutes')
(21)秒数-1
moment().subtract(1,'seconds')
3.格式化时间
| 格式代码 | 说明 | 返回值例子 |
|---|---|---|
| M | 数字表示的月份,没有前导零 | 1到12 |
| MM | 数字表示的月份,有前导零 | 01到12 |
| MMM | 三个字母缩写表示的月份 | Jan到Dec |
| MMMM | 月份,完整的文本格式 | January到December |
| Q | 季度 | 1到4 |
| D | 月份中的第几天,没有前导零 | 1到31 |
| DD | 月份中的第几天,有前导零 | 01到31 |
| d | 星期中的第几天,数字表示 | 0到6,0表示周日,6表示周六 |
| ddd | 三个字母表示星期中的第几天 | Sun到Sat |
| dddd | 星期几,完整的星期文本 | 从Sunday到Saturday |
| w | 年份中的第几周 | 如42:表示第42周 |
| YYYY | 四位数字完整表示的年份 | 如:2014 或 2000 |
| YY | 两位数字表示的年份 | 如:14 或 98 |
| A | 大写的AM PM | AM PM |
| a | 小写的am pm | am pm |
| HH | 小时,24小时制,有前导零 | 00到23 |
| H | 小时,24小时制,无前导零 | 0到23 |
| hh | 小时,12小时制,有前导零 | 00到12 |
| h | 小时,12小时制,无前导零 | 0到12 |
| m | 没有前导零的分钟数 | 0到59 |
| mm | 有前导零的分钟数 | 00到59 |
| s | 没有前导零的秒数 | 1到59 |
| ss | 有前导零的描述 | 01到59 |
| X | Unix时间戳 | 1411572969 |
moment().format('YYYY年MM月DD日')(2)格式化年月日: ‘xxxx-xx-xx’
moment().format('YYYY-MM-DD')(3)格式化时分秒(24小时制): ‘xx时xx分xx秒’
moment().format('HH时mm分ss秒')(4)格式化时分秒(12小时制):‘xx:xx:xx am/pm’
moment().format('hh:mm:ss a')(5)格式化时间戳(以毫秒为单位)
moment().format('x') // 返回值为字符串类型4.比较时间
(1)获取两个日期之间的时间差
let start\_date =moment().subtract(1,'weeks') let end\_date = moment() end\_date.diff(start\_date) // 返回毫秒数 end\_date.diff(start\_date,'months') // 月 end\_date.diff(start\_date,'weeks') // 周 end\_date.diff(start\_date,'days') // 天 start\_date.diff(end\_date,'days') // -7
(2)判断B日期是否在A日期前面
moment(B).isBefore(A)
5. 转化为JavaScript原生Date对象
moment().toDate() newDate(moment())
6.日期格式化
moment().format('MMMM Do YYYY, h:mm:ss a'); // 五月 24日 2019, 7:47:43 晚上
moment().format('dddd'); // 星期五
moment().format("MMM Do YY"); // 5月 24日 19
moment().format('YYYY \[escaped] YYYY'); // 2019 escaped 2019
moment().format(); // 2019-05-24T19:47:43+08:007.相对时间
moment("20111031","YYYYMMDD").fromNow(); // n年前
moment("20111031","YYYYMMDD").fromNow(true); // n年
moment().startOf('day').fromNow(); // n 小时前
moment().endOf('day').fromNow(); // n 小时内
moment().startOf('hour').fromNow(); // n 小时前下表概述了每个时间长度显示的字符串的细分。
| 范围 | 键 | 样本输出 |
|---|---|---|
| 0 至 44 秒 | s | 几秒前 |
| 未设定 | ss | 44 秒前 |
| 45 至 89 秒 | m | 1 分钟前 |
| 90 秒至 44 分钟 | mm | 2 分钟前 … 44 分钟前 |
| 45 至 89 分钟 | h | 1 小时前 |
| 90 分钟至 21 小时 | hh | 2 小时前 … 21 小时前 |
| 22 至 35 小时 | d | 1 天前 |
| 36 小时至 25 天 | dd | 2 天前 … 25 天前 |
| 26 至 45 天 | M | 1 个月前 |
| 45 至 319 天 | MM | 2 个月前 … 10 个月前 |
| 320 至 547 天 (1.5 年) | y | 1 年前 |
| 548 天+ | yy | 2 年前 … 20 年前 |
8.日历时间
moment().subtract(n,'days').calendar(); //当前时间的前n天时间 moment().calendar(); // 当前时间 moment().add(n,'days').calendar(); // 当前时间的后n天时间
9.多语言支持
moment().format('L'); moment().format('l'); // 月份数字,日期,年份 (09/04/1986)
moment().format('LL'); moment().format('ll'); //月份、月日、年 (1986 年 9 月 4 日)
moment().format('LLL'); moment().format('lll'); // 月份、月日、年、时间(1986 年 9 月 4 日 8:下午 30 点
moment().format('LLLL'); moment().format('llll'); // 月份名称、月份日期、星期几、年份、时间(1986 年 9 月 4 日星期四 8:下午 30 点)10.其它实用技巧:
moment().format("YYYY-MM-DD") // 格式化显示当前时间
moment().subtract("month", +1).format("YYYY-MM")-01 // 上一个月的1号
moment().add("month", -1).format("YYYY-MM")-01 // 还是上一个月1号
let M =`${moment().format("YYYY-MM")}-01` // 本月一号
moment(M).add("days", -1).format("YYYY-MM-DD") // 上一个月月底
moment().startOf("year").format("YYYY-MM-DD") // 本年的的开始日期
moment().endOf("year").format("YYYY-MM-DD") // 本年的的结束日期
moment().valueOf() // moment 转成时间戳
moment(string).format() // 时间戳 转 moment
// 解决Moment格式化时间出现时区差的问题
moment().utcOffset() // 接收数字,时间偏移量,单位:分钟
Moment(date).utcOffset(480).format('YYYY-MM-DD HH:mm:ss'); // 北京时间东八区时间,比零时区早8个小时(480分钟),所以应该加上480分钟到此这篇关于moment.js使用超详细教程的文章就介绍到这了,更多相关moment.js使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
