# 查询
# 是否之前
moment().isBefore(Moment|String|Number|Date|Array);
moment().isBefore(Moment|String|Number|Date|Array, String);
2
Check if a moment is before another moment.
moment('2010-10-20').isBefore('2010-10-21'); // true
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
moment('2010-10-20').isBefore('2011-01-01', 'year'); // true
2
Like moment#isAfter and moment#isSame, any of the units of time that are supported for moment#startOf are supported for
moment#isBefore.year month week day hour minute second.If nothing is passed to moment#isBefore, it will default to the current time.
- NOTE:
moment().isBefore()has undefined behavior and should not be used! If the code runs fast the initial created moment would be the same as the one created in isBefore to perform the check, so the result would be false. But if the code runs slower it's possible that the moment created in isBefore is measurably after the one created in moment(), so the call would return true.
# 是否相同
moment().isSame(Moment|String|Number|Date|Array);
moment().isSame(Moment|String|Number|Date|Array, String);
2
Check if a moment is the same as another moment.
moment('2010-10-20').isSame('2010-10-20'); // true
If you want to limit the granularity to a unit other than milliseconds, pass it 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
2
3
4
When including a second parameter, it will match all units equal or larger. Passing in month will check month and year. Passing in day will check day, month, and year.
moment('2010-01-01').isSame('2011-01-01', 'month'); // false, different year
moment('2010-01-01').isSame('2010-02-01', 'day'); // false, different month
2
Like moment#isAfter and moment#isBefore, any of the units of time that are supported for moment#startOf are supported for
moment#isSame.year month week day hour minute second
# 是否之后
moment().isAfter(Moment|String|Number|Date|Array);
moment().isAfter(Moment|String|Number|Date|Array, String);
2
Check if a moment is after another moment.
moment('2010-10-20').isAfter('2010-10-19'); // true
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
moment('2010-10-20').isAfter('2009-12-31', 'year'); // true
2
Like moment#isSame and moment#isBefore, any of the units of time that are supported for moment#startOf are supported for
moment#isAfter.year month week day hour minute second,If nothing is passed tomoment#isAfter, it will default to the current time.
moment().isAfter(); // false
# 是否之间
moment().isBetween(moment-like, moment-like);
moment().isBetween(moment-like, moment-like, String);
// where moment-like is Moment|String|Number|Date|Array
2
3
Check if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). The match is exclusive.
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second 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
2
Like moment#isSame, moment#isBefore, moment#isAfter any of the units of time that are supported for moment#startOf are supported for
moment#isBetween. Year, month, week, day, hour, minute, and second.
# 是否闰年
moment().isLeapYear();
moment#isLeapYear returns true if that year is a leap year, and false if it is not.
moment([2000]).isLeapYear() // true
moment([2001]).isLeapYear() // false
moment([2100]).isLeapYear() // false
2
3
# 是否夏令时
moment().isDST();
moment#isDSTchecks if the current moment is in daylight saving time.
moment([2011, 2, 12]).isDST(); // false, March 12 2011 is not DST
moment([2011, 2, 14]).isDST(); // true, March 14 2011 is DST
2
# 是否被夏令时转换
moment('2013-03-10 2:30', 'YYYY-MM-DD HH:mm').isDSTShifted()
Another important piece of validation is to know if the date has been moved by a DST. For example, in most of the United States:
moment('2013-03-10 2:30', 'YYYY-MM-DD HH:mm').format(); //=> '2013-03-10T01:30:00-05:00'
This is because daylight saving time shifts the time from 2:00 to 3:00, so 2:30 isn't a real time. The resulting time is browser-dependent, either adjusting the time forward or backwards.
Use moment#isDSTShifted to test for this condition.
- Note: before 2.3.0, Moment objects in this condition always returned false for moment#isValid; they now return true.
# 是否Moment对象
moment.isMoment(obj);
To check if a variable is a moment object, use moment.isMoment().
moment.isMoment() // false
moment.isMoment(new Date()) // false
moment.isMoment(moment()) // true
2
3
# 是否Date对象
moment.isDate(obj);
To check if a variable is a native js Date object, use moment.isDate().
moment.isDate(); // false
moment.isDate(new Date()); // true
moment.isDate(moment()); // false
2
3