Skip to content

Instantly share code, notes, and snippets.

@sumitridhal
Created December 2, 2020 18:28
Show Gist options
  • Save sumitridhal/0698ada2dc869a293bdc83a18125005f to your computer and use it in GitHub Desktop.
Save sumitridhal/0698ada2dc869a293bdc83a18125005f to your computer and use it in GitHub Desktop.
RegEx Date parse and validation
const f = 'MM/DD/YYYY';
const d = '11';
const arr = f.split('/');
const a = d.match(/([0-9]{1,2})?\/?([0-9]{1,2})?\/?([0-9]{2,4})?/);
const [DD,MM,YYYY] = [a[arr.indexOf('DD')+1] || 01, a[arr.indexOf('MM')+1] || 01,a[arr.indexOf('YYYY')+1] || 2001];
new Date(MM+'/'+DD+'/'+YYYY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment