多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[Moment.js 中文网 (momentjs.cn)](http://momentjs.cn/) #### 年份、月份、日期的令牌 令牌区分大小写。 | 输入 | 示例 | 描述 | | --- | --- | --- | | `YYYY` | `2014` | 4 或 2 位数字的年份 | | `YY` | `14` | 2 位数字的年份 | | `Y` | `-25` | 带有任意数字和符号的年份 | | `Q` | `1..4` | 年份的季度。将月份设置为季度的第一个月 | | `M MM` | `1..12` | 月份数字 | | `MMM MMMM` | `Jan..December` | 语言环境中的月份名称,由`moment.locale()`设置 | | `D DD` | `1..31` | 月的某天 | | `Do` | `1st..31st` | 月的某天,带序数 | | `DDD DDDD` | `1..365` | 年的某天 | | `X` | `1410715640.579` | Unix 时间戳 | | `x` | `1410715640579` | Unix 毫秒时间戳 | #### 周年、星期、工作日的令牌 对于这些,小写字母令牌使用语言环境感知的星期开始天数,而大写字母令牌使用[ISO 星期日期](http://nodejs.cn/s/MkHCug)开始天数。 令牌区分大小写。 | 输入 | 示例 | 描述 | | --- | --- | --- | | `gggg` | `2014` | 语言环境的 4 位数字的周年 | | `gg` | `14` | 语言环境的 2 位数字的周年 | | `w ww` | `1..53` | 语言环境的年的第几周 | | `e` | `0..6` | 语言环境的星期几 | | `ddd dddd` | `Mon...Sunday` | 语言环境的星期几的名称,由`moment.locale()`设置 | | `GGGG` | `2014` | ISO 的 4 位数字的周年 | | `GG` | `14` | ISO 的 2 位数字的周年 | | `W WW` | `1..53` | ISO 的年的第几周 | | `E` | `1..7` | ISO 的星期几 | #### 语言环境感知的格式 使用`LT LTS L LL LLL LLLL`也可以使用语言环境感知的日期和时间格式。 它们新增于**2.2.1**版本,其中`LTS`新增于**2.8.4**。 令牌区分大小写。 | 输入 | 示例 | 描述 | | --- | --- | --- | | `L` | `04/09/1986` | 日期(以本地格式) | | `LL` | `September 4 1986` | 月份名称、月份日期、年份 | | `LLL` | `September 4 1986 8:30 PM` | 月份名称、月份日期、年份、时间 | | `LLLL` | `Thursday, September 4 1986 8:30 PM` | 星期几、月份名称、月份日期、年份、时间 | | `LT` | `08:30 PM` | 时间(不含秒钟) | | `LTS` | `08:30:00 PM` | 时间(含秒钟) | #### 小时、分钟、秒种、毫秒、偏移量的令牌 令牌区分大小写。 | 输入 | 示例 | 描述 | | --- | --- | --- | | `H HH` | `0..23` | 小时(24 小时制) | | `h hh` | `1..12` | 小时(使用`a A`的 12 小时制) | | `k kk` | `1..24` | 小时(从 1 到 24 的 24 小时制) | | `a A` | `am pm` | 上午或下午(单一字符`a p`也被视为有效) | | `m mm` | `0..59` | 分钟 | | `s ss` | `0..59` | 秒钟 | | `S SS SSS` | `0..999` | 带分数的秒钟 | | `Z ZZ` | `+12:00` | 从 UTC 偏移为`+-HH:mm`、`+-HHmm`或`Z` | Moment.js 使用重载的 getter 和 setter 方法。 此模式类似与其在 jQuery 中的使用。 不带参数调用这些方法会作为 getter,而带参数调用则会作为 setter。 这些会映射到原生`Date`对象上的相应函数。 ~~~javascript moment().seconds(30).valueOf() === new Date().setSeconds(30); moment().seconds() === new Date().getSeconds(); ~~~ 如果处于[UTC 模式](http://nodejs.cn/s/t4J2Y4)中,则它们将会映射到 UTC 的等效项。 ~~~javascript moment.utc().seconds(30).valueOf() === new Date().setUTCSeconds(30); moment.utc().seconds() === new Date().getUTCSeconds(); ~~~ >[danger]如果链接多个操作以构造一个日期,则应从年份、月份、日期等依次开始。 否则,可能会得到意外的结果 错误:`moment().date(day).month(month).year(year)` 正确:`moment().year(year).month(month).date(day)` [millisecond()](http://momentjs.cn/docs/#/get-set/millisecond/) 获取或设置毫秒 接受 0 到 999 之间的数字。 如果超出范围,则它将会冒泡到秒钟 [second()](http://momentjs.cn/docs/#/get-set/second/)获取或设置秒钟 接受 0 到 59 之间的数字。 如果超出范围,则它将会冒泡到分钟 [minute()](http://momentjs.cn/docs/#/get-set/minute/)获取或设置分钟 接受 0 到 59 之间的数字。 如果超出范围,则它将会冒泡到小时。 [hour()](http://momentjs.cn/docs/#/get-set/hour/)获取或设置小时 接受 0 到 23 之间的数字。 如果超出范围,则它将会冒泡到日期 [date()](http://momentjs.cn/docs/#/get-set/date/)获取或设置月份的日期 接受 1 到 31 之间的数字。 如果超出范围,则它将会冒泡达到月份 [day()](http://momentjs.cn/docs/#/get-set/day/)获取或设置星期几 此方法可用于设置星期几,其中星期日为 0、星期六为 6 如果超出范围,则它将会冒泡到其他星期 [weekday()](http://momentjs.cn/docs/#/get-set/weekday/)根据语言环境获取或设置星期几 [isoWeekday()](http://momentjs.cn/docs/#/get-set/iso-weekday/)获取或设置[ISO 星期几](http://nodejs.cn/s/MkHCug),其中`1`是星期一、`7`是星期日 [dayOfYear()](http://momentjs.cn/docs/#/get-set/day-of-year/)获取或设置年份的日期 接受 1 到 366 之间的数字。 如果超出范围,则它将会冒泡到年份 [week()](http://momentjs.cn/docs/#/get-set/week/)获取或设置年份的星期 [isoWeek()](http://momentjs.cn/docs/#/get-set/iso-week/)获取或设置[年份的 ISO 星期](http://nodejs.cn/s/MkHCug) [month()](http://momentjs.cn/docs/#/get-set/month/)获取或设置月份 [quarter()](http://momentjs.cn/docs/#/get-set/quarter/)获取或设置季度 [year()](http://momentjs.cn/docs/#/get-set/year/)获取或设置年份 [weekYear()](http://momentjs.cn/docs/#/get-set/week-year/)根据语言环境获取或设置周年 [isoWeekYear()](http://momentjs.cn/docs/#/get-set/iso-week-year/)获取或设置[ISO 周年](http://nodejs.cn/s/MkHCug) [weeksInYear()](http://momentjs.cn/docs/#/get-set/weeks-in-year/)根据语言环境获取当前 moment 年份的周数。 [isoWeeksInYear()](http://momentjs.cn/docs/#/get-set/iso-weeks-in-year/)根据[ISO 星期](http://nodejs.cn/s/MkHCug)获取当前 moment 年份的周数 [get()](http://momentjs.cn/docs/#/get-set/get/) [set()](http://momentjs.cn/docs/#/get-set/set/) [max()](http://momentjs.cn/docs/#/get-set/max/)返回给定的 moment 实例的最大值 [min()](http://momentjs.cn/docs/#/get-set/min/)返回给定的 moment 实例的最小值 ## [操作](http://momentjs.cn/docs/#/manipulating/) [add()](http://momentjs.cn/docs/#/manipulating/add/) 通过增加时间来改变原始的 moment [subtract()](http://momentjs.cn/docs/#/manipulating/subtract/) 通过减去时间来改变原始的 moment [startOf()](http://momentjs.cn/docs/#/manipulating/start-of/) 通过将原始的 moment 设置为时间单位的开头来对其进行更改(月日分秒等的最小值) [endOf()](http://momentjs.cn/docs/#/manipulating/end-of/) 通过将原始的 moment 设置为时间单位的末尾来对其进行更改(月日分秒等的最大值) [max()](http://momentjs.cn/docs/#/manipulating/max/) [min()](http://momentjs.cn/docs/#/manipulating/min/) [local()](http://momentjs.cn/docs/#/manipulating/local/) 在原始的 moment 上设置标记,以使用本地时间(而不是原始的 moment 时间)显示 moment [utc()](http://momentjs.cn/docs/#/manipulating/utc/) 在原始的 moment 上设置标记,以使用 UTC(而不是原始的 moment 时间)显示 moment [utcOffset()](http://momentjs.cn/docs/#/manipulating/utc-offset/) 取 UTC 偏移量(以分钟为单位) [zone()](http://momentjs.cn/docs/#/manipulating/timezone-offset/) ## [显示](http://momentjs.cn/docs/#/displaying/) [format()](http://momentjs.cn/docs/#/displaying/format/) ``` var day = moment("1995-12-25");//Mon Dec 25 1995 00:00:00 GMT+0800 var now =moment().format(); var now =moment("12/25/1995", "MM-DD-YYYY").format();//1995-12-25T00:00:00+08:00 var now =moment('2012 July', 'YYYY MMM', 'en').format();//1995-12-25T00:00:00+08:00 //如果不知道输入字符串的确切格式,但是知道它可能是多种格式之一,则可以使用格式数组 var now =moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]).format();//1995-12-25T00:00:00+08:00 var now =moment(1318781876406).format();//毫秒级2011-10-17T00:17:56+08:00 var now =moment.unix(1318781876).format();//秒级2011-10-17T00:17:56+08:00 //[year, month, day, hour, minute, second, millisecond] 里面的几个参数都可以省略 var now =moment([2010, 1, 14, 15, 25, 50, 125]).format();//2010-02-14T15:25:50+08:00 var now =moment([]).format();//空数组就是当前时间2021-09-26T09:07:27+08:00 ``` ``` //本月第一天 moment().startOf('month').format('YYYY-MM-DD'); //2020-12-01 //本月最后一天 moment().endOf('month').format('YYYY-MM-DD'); //2020-12-31 //上个月第一天 moment().subtract(1,'month').startOf('month').format('YYYY-MM-DD') //2020-11-01 //上月最后一天 moment().subtract(1,'month').endOf('month').format('YYYY-MM-DD') //2020-11-30 //今年第一天 moment().startOf('year').format('YYYY-MM-DD') //2020-01-01 //去年第一天 moment().subtract(1,'year').startOf('year').format('YYYY-MM-DD') //2019-01-01 //去年后一天 moment().subtract(1,'year').endOf('year').format('YYYY-MM-DD') //2019-12-31 //一年前 moment().subtract(1,'year').format('YYYY-MM-DD'); //2019-12-02 //昨天 moment().subtract(1,'day').format('YYYY-MM-DD') //2020-12-01 //今天 moment().format('YYYY-MM-DD') //2020-12-02 ```