fix: use more versatile date regex
Previous implementation could not parse dates that are lacking delimiters. Fix issue #116
This commit is contained in:
parent
7e4728d646
commit
2897b647a1
1 changed files with 16 additions and 1 deletions
|
@ -31,6 +31,18 @@ let dateFormats = [
|
||||||
{ format: 'dd mm yy', label: 'DD MM YY' }
|
{ format: 'dd mm yy', label: 'DD MM YY' }
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const reYearFirst = /^(\d{4})(?:[^\d]*)?(\d{2})(?:[^\d]*)?(\d{2})$/;
|
||||||
|
const reYearLast = /^(\d{2})(?:[^\d]*)?(\d{2})(?:[^\d]*)?(\d{4})$/;
|
||||||
|
const reTwoDig = /^(\d{2})(?:[^\d]*)?(\d{2})(?:[^\d]*)?(\d{2})$/;
|
||||||
|
const re = {
|
||||||
|
'yyyy mm dd': reYearFirst,
|
||||||
|
'mm dd yyyy': reYearLast,
|
||||||
|
'dd mm yyyy': reYearLast,
|
||||||
|
'yy mm dd': reTwoDig,
|
||||||
|
'mm dd yy': reTwoDig,
|
||||||
|
'dd mm yy': reTwoDig
|
||||||
|
};
|
||||||
|
|
||||||
function parseDate(str, order) {
|
function parseDate(str, order) {
|
||||||
if (typeof str !== 'string') {
|
if (typeof str !== 'string') {
|
||||||
return null;
|
return null;
|
||||||
|
@ -40,7 +52,10 @@ function parseDate(str, order) {
|
||||||
return v && v.length === 1 ? '0' + v : v;
|
return v && v.length === 1 ? '0' + v : v;
|
||||||
}
|
}
|
||||||
|
|
||||||
let parts = str.replace(/ /g, '').split(/[^0-9]/);
|
let parts = str.replace(/\s+/g, '').match(re[order]);
|
||||||
|
if (!parts) return null;
|
||||||
|
|
||||||
|
parts = parts.slice(1);
|
||||||
|
|
||||||
let year, month, day;
|
let year, month, day;
|
||||||
switch (order) {
|
switch (order) {
|
||||||
|
|
Loading…
Reference in a new issue