当前位置:网站首页>Moment.js
Moment.js
2022-07-25 23:21:00 【Intern Sen】
Moment.js (JavaScript Date processing class library ) Official website
One 、 Date formatting
moment().format('MMMM Do YYYY, h:mm:ss a'); // July 7 Japan 2022, 11:27:31 In the morning
moment().format('dddd'); // Thursday
moment().format("MMM Do YY"); // 7 month 7 Japan 22
moment().format('YYYY [escaped] YYYY'); // 2022 escaped 2022
moment().format(); // 2022-07-07T11:27:31+08:00
Two 、 Relative time
moment("20111031", "YYYYMMDD").fromNow(); // 11 Years ago
moment("20120620", "YYYYMMDD").fromNow(); // 10 Years ago
moment().startOf('day').fromNow(); // 11 Hours before
moment().endOf('day').fromNow(); // 13 Within hours
moment().startOf('hour').fromNow(); // 28 Minutes ago
3、 ... and 、 Calendar time
moment().subtract(10, 'days').calendar(); // 2022/06/27
moment().subtract(6, 'days').calendar(); // Last Friday 11:27
moment().subtract(3, 'days').calendar(); // Last Monday 11:27
moment().subtract(1, 'days').calendar(); // yesterday 11:27
moment().calendar(); // today 11:27
moment().add(1, 'days').calendar(); // Tomorrow, 11:27
moment().add(3, 'days').calendar(); // Next Sunday 11:27
moment().add(10, 'days').calendar(); // 2022/07/17
Four 、 Multilingual support
moment.locale(); // zh-cn
moment().format('LT'); // 11:27
moment().format('LTS'); // 11:27:31
moment().format('L'); // 2022/07/07
moment().format('l'); // 2022/7/7
moment().format('LL'); // 2022 year 7 month 7 Japan
moment().format('ll'); // 2022 year 7 month 7 Japan
moment().format('LLL'); // 2022 year 7 month 7 The morning of 11 spot 27 branch
moment().format('lll'); // 2022 year 7 month 7 Japan 11:27
moment().format('LLLL'); // 2022 year 7 month 7 Sunday, Thursday morning 11 spot 27 branch
moment().format('llll'); // 2022 year 7 month 7 Thursday, Sunday 11:27
5、 ... and 、 Get the current time
moment().get('year'); // 2022( year )
moment().get('month'); // 6 0 to 11( month )
moment().get('date'); // 7 ( date )
moment().get('hour'); // 13( Hours )
moment().get('minute'); // 14( minute )
moment().get('second'); // 15( second )
moment().get('millisecond'); // 222 millisecond
6、 ... and 、 Inquire about
6.1 isBefore()
Check one moment Is it in another moment Before .
The first parameter will be resolved to moment ( If not resolved ).
moment('2010-10-20').isBefore('2010-10-21'); // true
If you want to limit the granularity to units other than milliseconds , Then pass the unit as the second parameter .
because The second parameter is used to determine the accuracy , It's not just about checking individual values , Therefore use day The year... Will be checked 、 month 、 date .
moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
moment('2010-10-20').isBefore('2011-01-01', 'year'); // true
And moment#isAfter and moment#isSame equally ,moment#startOf Any time unit supported also applies to moment#isBefore.
year month week isoWeek day hour minute second
6.2 isSame()
Check one moment Whether with another moment identical .
The first parameter will be resolved to moment( If not resolved ).
moment('2010-10-20').isSame('2010-10-20'); // true
If you want to limit the granularity to units other than milliseconds , Then pass the unit as the second parameter .
moment('2010-10-20').isSame('2009-12-31', 'year'); // false
moment('2010-10-20').isSame('2010-01-01', 'year'); // true
moment('2010-10-20').isSame('2010-12-31', 'year'); // true
moment('2010-10-20').isSame('2011-01-01', 'year'); // false
When the second parameter is included , Then it will match all units equal to or greater . Pass in month Will check month and year. Pass in day Will check day、month and year.
moment('2010-01-01').isSame('2011-01-01', 'month'); // false, Different years
moment('2010-01-01').isSame('2010-02-01', 'day'); // false, Different months
6.3 isAfter()
Check one moment Is it in another moment after .
The first parameter will be resolved to moment( If not resolved ).
moment('2010-10-20').isAfter('2010-10-19'); // true
If you want to limit the granularity to units other than milliseconds , Then pass the unit as the second parameter .
Because the second parameter is used to determine the accuracy , And not just a single value to check , Therefore use day The year... Will be checked 、 month 、 date .
moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
moment('2010-10-20').isAfter('2009-12-31', 'year'); // true
And moment#isSame and moment#isBefore equally ,moment#startOf Any time unit supported also applies to moment#isAfter.
year month week isoWeek day hour minute second
If nothing is passed to moment#isAfter, Then it will default to the current time .
moment().isAfter(); // false
6.4 isSameOrBefore()
Check one moment Is it in another moment Before or the same .
The first parameter will be resolved to moment( If not resolved ).
moment('2010-10-20').isSameOrBefore('2010-10-21'); // true
moment('2010-10-20').isSameOrBefore('2010-10-20'); // true
moment('2010-10-20').isSameOrBefore('2010-10-19'); // false
If you want to limit the granularity to units other than milliseconds , Then pass the unit as the second parameter .
Because the second parameter is used to determine the accuracy , And not just a single value to check , Therefore use day The year... Will be checked 、 month 、 date .
moment('2010-10-20').isSameOrBefore('2009-12-31', 'year'); // false
moment('2010-10-20').isSameOrBefore('2010-12-31', 'year'); // true
moment('2010-10-20').isSameOrBefore('2011-01-01', 'year'); // true
6.5 isSameOrAfter()
Check one moment Is it in another moment Later or the same . The first parameter will be resolved to moment( If not resolved ).
moment('2010-10-20').isSameOrAfter('2010-10-19'); // true
moment('2010-10-20').isSameOrAfter('2010-10-20'); // true
moment('2010-10-20').isSameOrAfter('2010-10-21'); // false
If you want to limit the granularity to units other than milliseconds , Then pass the unit as the second parameter .
Because the second parameter is used to determine the accuracy , And not just a single value to check , Therefore use day The year... Will be checked 、 month 、 date .
moment('2010-10-20').isSameOrAfter('2011-12-31', 'year'); // false
moment('2010-10-20').isSameOrAfter('2010-01-01', 'year'); // true
moment('2010-10-20').isSameOrAfter('2009-12-31', 'year'); // true
6.5 isBetween()
Check one moment Whether in the other two moment Between , Optionally check the specified unit scale ( minute , Hours , Date, etc. ). This match is exclusive . The first two parameters will be resolved as moment( If not resolved ).
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
moment('2010-10-20').isBetween('2010-10-19', undefined); // true, because moment(undefined) Equivalent to moment()
If you want to limit the granularity to units other than milliseconds , Then pass the unit as the third parameter .
moment('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false
moment('2010-10-20').isBetween('2009-12-31', '2012-01-01', 'year'); // true
And moment#isSame、moment#isBefore、moment#isAfter equally ,moment#startOf Any time unit supported also applies to moment#isBetween. year 、 month 、 week 、ISO week 、 date 、 Hours 、 minute 、 Second .
2.13.0 Version introduces inclusiveness . [ Means to contain . ( To exclude . If inclusive parameters are used , Then two indicators must be passed .
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //true
Be careful , If from and to Parameters are the same , But the inclusive parameters are different , Will return false.
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '(]'); //false
If the inclusive parameter is not specified , be Moment Will default to ().
6.6 isDST()
moment().isDST();
moment#isDST Check whether the current time is summer time (daylight saving time).
moment([2011, 2, 12]).isDST(); // false, 2011 year 3 month 12 Day is not DST.
moment([2011, 2, 14]).isDST(); // true, 2011 year 3 month 14 The day is DST.
// This example applies to "en" Language environment :https://www.timeanddate.com/time/dst/2011.html
6.7 isLeapYear()
moment().isLeapYear();
If the year is a leap year , be moment#isLeapYear return true, Otherwise return to false.
moment([2000]).isLeapYear() // true
moment([2001]).isLeapYear() // false
moment([2100]).isLeapYear() // false
6.8 isMoment()
moment.isMoment(obj);
To check whether the variable is moment object , Then use moment.isMoment().
moment.isMoment() // false
moment.isMoment(new Date()) // false
moment.isMoment(moment()) // true
from 2.11.0 Version start , You can also use instanceof Operator detection moment object :
moment() instanceof moment // true
6.9 isDate()
moment.isDate(obj);
To check whether the variable is native js Date object , Then use moment.isDate().
moment.isDate(); // false
moment.isDate(new Date()); // true
moment.isDate(moment()); // false
边栏推荐
- 连续三年成为云AI服务领导者,亚马逊云科技做对了什么?
- General paging function
- WordPress function encyclopedia, you can do the theme after learning it
- [QNX hypervisor 2.2 user manual]9.6 GDB
- Pytorch data input format requirements and conversion
- MES系统设备管理概述(下)
- Thinkphp6 temporarily close the layout
- Tencent map API request source is not authorized, this request source domain name
- Hj9 extract non duplicate integers
- Enterprise level inventory management system of code audit
猜你喜欢

Classes and objects (3)

Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK

The VM session was closed before any attempt to power it on

Network Security Learning notes-1 file upload

Tips for using (1)

Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants

AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario

数组中重复的数字

General paging function

VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
随机推荐
Redis expiration key deletion strategy [easy to understand]
Secure code warrior learning record (IV)
Solution of phpstudy service environment 80 port occupied by process system under Windows
The VM session was closed before any attempt to power it on
赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
Wechat official account, wechat payment development
Unity uses macros
MathType installation and solution cannot solve the problem of crtl+v
Details of notification (status bar notification)
PHP binary array is sorted by a field in it
idea设置get、set模板解决boolean类型字段的命名问题
Pytorch data input format requirements and conversion
XXE&XML-外部实体注入-利用和绕过
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
POI special effects Market Research
Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
TS basic data type
Secure code warrior learning record (III)
向下扎根,向上生长,探寻华为云AI的“根”力量
Analysis of direction finding error of multi baseline interferometer system