Skip to content

Instantly share code, notes, and snippets.

@dwhiteGUK
Last active May 11, 2020 11:03
Show Gist options
  • Save dwhiteGUK/ad7fe6e8b2e42742a3b099dfdbb9dacf to your computer and use it in GitHub Desktop.
Save dwhiteGUK/ad7fe6e8b2e42742a3b099dfdbb9dacf to your computer and use it in GitHub Desktop.
Working with JS Date and Date-fns library
// Max Date
const { format, isBefore, isValid, subYears, parseISO } = require('date-fns');
const maxDate = format(subYears(new Date(), 18), 'yyyy-MM-dd');
const maxResult = isBefore(dobDate, parseISO(maxDate, 1));
// Min Date
// Requires Date FNS
const { format, isAfter, subYears, parseISO } = require('date-fns');
const minDate = format(subYears(new Date(), 150), 'yyyy-MM-dd');
const maxResult = isAfter(dobDate, parseISO(maxDate, 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment