From 15ea2ba913cd2c638a7b50fa81f590bc56d5d5fc Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 23 Sep 2020 08:22:40 -0700 Subject: [PATCH] Fix date issue in Safari. --- lib/date.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/date.js b/lib/date.js index 63bad45e..cdfe322c 100644 --- a/lib/date.js +++ b/lib/date.js @@ -100,6 +100,19 @@ export function getDateRangeValues(startDate, endDate) { return { startDate: startOfDay(startDate), endDate: endOfDay(endDate), unit }; } +export function getDateFromString(str) { + const [ymd, hms] = str.split(' '); + const [year, month, day] = ymd.split('-'); + + if (hms) { + const [hour, min, sec] = hms.split(':'); + + return new Date(year, month - 1, day, hour, min, sec); + } + + return new Date(year, month - 1, day); +} + const dateFuncs = { hour: [differenceInHours, addHours, startOfHour], day: [differenceInCalendarDays, addDays, startOfDay], @@ -114,12 +127,7 @@ export function getDateArray(data, startDate, endDate, unit) { function findData(t) { const x = data.find(e => { - if (unit === 'hour') { - return normalize(new Date(e.t)).getTime() === t.getTime(); - } - - const [year, month, day] = e.t.split('-'); - return normalize(new Date(year, month - 1, day)).getTime() === t.getTime(); + return normalize(getDateFromString(e.t)).getTime() === t.getTime(); }); return x?.y || 0;