当前位置:网站首页>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
边栏推荐
- POI特效 市场调研
- Wamp MySQL empty password
- Several commonly used traversal methods
- Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
- [interface performance optimization] reasons for index failure and how to optimize SQL
- TS basic data type
- ffmpeg初次学习(仅针对编码)
- Family relationship calculator wechat applet source code
- 物理防火墙是什么?有什么作用?
- 通用分页功能
猜你喜欢

电商RPA,大促轻松上阵的法宝

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化

Zero crossing position search of discrete data (array)

serialization and deserialization

Drive board network cable directly connected to computer shared network configuration
![[interface performance optimization] reasons for index failure and how to optimize SQL](/img/b9/64058c823c4497ac36bfb62a101816.jpg)
[interface performance optimization] reasons for index failure and how to optimize SQL

About using NPM command under the terminal, the installation error problem is solved (my own experience)

类和对象(3)

驱动板网线直连电脑共享网络配置

Tencent map API request source is not authorized, this request source domain name
随机推荐
通用分页功能
PHP JSON variable array problem
Recommended system - an embedded learning framework for numerical features in CTR prediction
Enterprise level inventory management system of code audit
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
serialization and deserialization
Secure code warrior learning record (II)
XXE&XML-外部实体注入-利用和绕过
日期类的实现
Single model common sense reasoning first surpasses human beings! HFL summit openbookqa challenge
Week 2: convolutional neural network
POI特效 市场调研
@Import
What has Amazon cloud technology done right to become the leader of cloud AI services for three consecutive years?
Tencent map API request source is not authorized, this request source domain name
Pytorch data input format requirements and conversion
四旋翼飞行器的飞控实现「建议收藏」
firewall 命令简单操作
Npm+ module loading mechanism
Rendering, filtering (filtering) and sorting of lists