compare.js (2379B)
1import { isMoment } from './constructor'; 2import { normalizeUnits } from '../units/aliases'; 3import { createLocal } from '../create/local'; 4 5export function isAfter(input, units) { 6 var localInput = isMoment(input) ? input : createLocal(input); 7 if (!(this.isValid() && localInput.isValid())) { 8 return false; 9 } 10 units = normalizeUnits(units) || 'millisecond'; 11 if (units === 'millisecond') { 12 return this.valueOf() > localInput.valueOf(); 13 } else { 14 return localInput.valueOf() < this.clone().startOf(units).valueOf(); 15 } 16} 17 18export function isBefore(input, units) { 19 var localInput = isMoment(input) ? input : createLocal(input); 20 if (!(this.isValid() && localInput.isValid())) { 21 return false; 22 } 23 units = normalizeUnits(units) || 'millisecond'; 24 if (units === 'millisecond') { 25 return this.valueOf() < localInput.valueOf(); 26 } else { 27 return this.clone().endOf(units).valueOf() < localInput.valueOf(); 28 } 29} 30 31export function isBetween(from, to, units, inclusivity) { 32 var localFrom = isMoment(from) ? from : createLocal(from), 33 localTo = isMoment(to) ? to : createLocal(to); 34 if (!(this.isValid() && localFrom.isValid() && localTo.isValid())) { 35 return false; 36 } 37 inclusivity = inclusivity || '()'; 38 return ( 39 (inclusivity[0] === '(' 40 ? this.isAfter(localFrom, units) 41 : !this.isBefore(localFrom, units)) && 42 (inclusivity[1] === ')' 43 ? this.isBefore(localTo, units) 44 : !this.isAfter(localTo, units)) 45 ); 46} 47 48export function isSame(input, units) { 49 var localInput = isMoment(input) ? input : createLocal(input), 50 inputMs; 51 if (!(this.isValid() && localInput.isValid())) { 52 return false; 53 } 54 units = normalizeUnits(units) || 'millisecond'; 55 if (units === 'millisecond') { 56 return this.valueOf() === localInput.valueOf(); 57 } else { 58 inputMs = localInput.valueOf(); 59 return ( 60 this.clone().startOf(units).valueOf() <= inputMs && 61 inputMs <= this.clone().endOf(units).valueOf() 62 ); 63 } 64} 65 66export function isSameOrAfter(input, units) { 67 return this.isSame(input, units) || this.isAfter(input, units); 68} 69 70export function isSameOrBefore(input, units) { 71 return this.isSame(input, units) || this.isBefore(input, units); 72}