Created
July 24, 2019 21:33
-
-
Save whatl3y/ab02c35ed824cc57904d04812287fdbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import moment from 'moment' | |
export default { | |
parse(dueDateProvided, remainingFormats=Object.keys(this.formats)) { | |
if (remainingFormats.length === 0) | |
return null | |
const [ format ] = remainingFormats.splice(0, 1) | |
if (typeof dueDateProvided === 'string' && !this.formats[format].regexp.test(dueDateProvided)) | |
return this.parse(dueDateProvided, remainingFormats) | |
const date = moment(dueDateProvided, format) | |
if (!date.isValid()) | |
return this.parse(dueDateProvided, remainingFormats) | |
return date.toDate() | |
}, | |
formats: { | |
'YYYY-MM-DD': { regexp: /\d{4}-\d{1,2}-\d{1,2}/ }, | |
'MM/DD/YYYY': { regexp: /\d{1,2}\/\d{1,2}\/\d{4}/ }, | |
'MM-DD-YYYY': { regexp: /\d{1,2}-\d{1,2}-\d{4}/ } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment