Query

Is Before

Check if a date is before another date.

  1. // Moment.js
  2. moment('2010-10-20').isBefore('2010-10-21');
  3. // => true
  4. // Native
  5. new Date(2010, 10, 20) < new Date(2010, 10, 21);
  6. // => true
  7. // date-fns
  8. import isBefore from 'date-fns/isBefore';
  9. isBefore(new Date(2010, 9, 20), new Date(2010, 9, 21));
  10. // => true
  11. // dayjs
  12. dayjs('2010-10-20').isBefore('2010-10-21');
  13. // => true
  14. // luxon
  15. DateTime.fromISO('2010-10-20') < DateTime.fromISO('2010-10-21');
  16. // => true

Is Same

Check if a date is the same as another date.

  1. // Moment.js
  2. moment('2010-10-20').isSame('2010-10-21');
  3. // => false
  4. moment('2010-10-20').isSame('2010-10-20');
  5. // => true
  6. moment('2010-10-20').isSame('2010-10-21', 'month');
  7. // => true
  8. // Native
  9. new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 21).valueOf();
  10. // => false
  11. new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).valueOf();
  12. // => true
  13. new Date(2010, 9, 20).getTime() === new Date(2010, 9, 20).getTime();
  14. // => true
  15. new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).getTime();
  16. // => true
  17. new Date(2010, 9, 20).toDateString().substring(4, 7) ===
  18. new Date(2010, 9, 21).toDateString().substring(4, 7);
  19. // => true
  20. // date-fns
  21. import isSameDay from 'date-fns/isSameDay';
  22. import isSameMonth from 'date-fns/isSameMonth';
  23. isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 21));
  24. // => false
  25. isSameDay(new Date(2010, 9, 20), new Date(2010, 9, 20));
  26. // => true
  27. isSameMonth(new Date(2010, 9, 20), new Date(2010, 9, 21));
  28. // => true
  29. // dayjs
  30. dayjs('2010-10-20').isSame('2010-10-21');
  31. // => false
  32. dayjs('2010-10-20').isSame('2010-10-20');
  33. // => true
  34. dayjs('2010-10-20').isSame('2010-10-21', 'month');
  35. // => true
  36. // luxon
  37. (+DateTime.fromISO('2010-10-20') ===
  38. +DateTime.fromISO('2010-10-21') +
  39. // => false
  40. DateTime.fromISO('2010-10-20')) ===
  41. +DateTime.fromISO('2010-10-20');
  42. // => true
  43. DateTime.fromISO('2010-10-20').hasSame(DateTime.fromISO('2010-10-21'), 'month');
  44. // => true

Is After

Check if a date is after another date.

  1. // Moment.js
  2. moment('2010-10-20').isAfter('2010-10-19');
  3. // => true
  4. // Native
  5. new Date(2010, 9, 20) > new Date(2010, 9, 19);
  6. // => true
  7. // date-fns
  8. import isAfter from 'date-fns/isAfter';
  9. isAfter(new Date(2010, 9, 20), new Date(2010, 9, 19));
  10. // => true
  11. // dayjs
  12. dayjs('2010-10-20').isAfter('2010-10-19');
  13. // => true
  14. // luxon
  15. DateTime.fromISO('2010-10-20') > DateTime.fromISO('2010-10-19');
  16. // => true

Is Between

Check if a date is between two other dates.

  1. // Moment.js
  2. moment('2010-10-20').isBetween('2010-10-19', '2010-10-25');
  3. // => true
  4. // date-fns
  5. import isWithinInterval from 'date-fns/isWithinInterval';
  6. isWithinInterval(new Date(2010, 9, 20), {
  7. start: new Date(2010, 9, 19),
  8. end: new Date(2010, 9, 25),
  9. });
  10. // => true
  11. // dayjs ⚠️ requires isBetween plugin
  12. import isBetween from 'dayjs/plugin/isBetween';
  13. dayjs.extend(isBetween);
  14. dayjs('2010-10-20').isBetween('2010-10-19', '2010-10-25');
  15. // => true
  16. // luxon
  17. Interval.fromDateTimes(
  18. DateTime.fromISO('2010-10-19'),
  19. DateTime.fromISO('2010-10-25')
  20. ).contains(DateTime.fromISO('2010-10-20'));
  21. // => true

Is Leap Year

Check if a year is a leap year.

  1. // Moment.js
  2. moment([2000]).isLeapYear();
  3. // => true
  4. // Native
  5. new Date(2000, 1, 29).getDate() === 29;
  6. // => true
  7. // date-fns
  8. import isLeapYear from 'date-fns/isLeapYear';
  9. isLeapYear(new Date(2000, 0, 1));
  10. // => true
  11. // dayjs ⚠️ requires isLeapYear plugin
  12. import isLeapYear from 'dayjs/plugin/isLeapYear';
  13. dayjs.extend(isLeapYear);
  14. dayjs('2000-01-01').isLeapYear();
  15. // => true
  16. // luxon
  17. expect(DateTime.local(2000).isInLeapYear).toBeTruthy();
  18. // => true

Is a Date

Check if a variable is a native js Date object.

  1. // Moment.js
  2. moment.isDate(new Date());
  3. // => true
  4. // Native
  5. new Date() instanceof Date;
  6. // => true
  7. // date-fns
  8. import isDate from 'date-fns/isDate';
  9. isDate(new Date());
  10. // => true
  11. // dayjs
  12. dayjs(new Date()).isValid()
  13. // luxon
  14. DateTime.local().isValid;
  15. // => true