Skip to content
Snippets Groups Projects
Commit 2897b647 authored by Arthur E. Jones's avatar Arthur E. Jones Committed by James Long
Browse files

fix: use more versatile date regex

Previous implementation could not parse dates that are lacking delimiters.

Fix issue #116
parent 7e4728d6
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,18 @@ let dateFormats = [ ...@@ -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) { ...@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment