improve date query parsing (#1992) (#2296)

Co-authored-by: Omar López <zomars@me.com>
pull/2303/head^2
buschco 2022-03-28 20:50:31 +02:00 committed by GitHub
parent bd66ca183f
commit cc5537dd1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 6 deletions

View File

@ -59,12 +59,18 @@ const AvailabilityPage = ({ profile, eventType, workingHours, previousPage }: Pr
const selectedDate = useMemo(() => { const selectedDate = useMemo(() => {
const dateString = asStringOrNull(router.query.date); const dateString = asStringOrNull(router.query.date);
if (dateString) { if (dateString) {
// todo some extra validation maybe. const offsetString = dateString.substr(11, 14); // hhmm
const utcOffsetAsDate = dayjs(dateString.substr(11, 14), "Hmm"); const offsetSign = dateString.substr(10, 1); // + or -
const utcOffset = parseInt(
dateString.substr(10, 1) + (utcOffsetAsDate.hour() * 60 + utcOffsetAsDate.minute()) const offsetHour = offsetString.slice(0, -2);
); const offsetMinute = offsetString.slice(-2);
const date = dayjs(dateString.substr(0, 10)).utcOffset(utcOffset, true);
const utcOffsetInMinutes =
(offsetSign === "-" ? -1 : 1) *
(60 * (offsetHour !== "" ? parseInt(offsetHour) : 0) +
(offsetMinute !== "" ? parseInt(offsetMinute) : 0));
const date = dayjs(dateString.substr(0, 10)).utcOffset(utcOffsetInMinutes, true);
return date.isValid() ? date : null; return date.isValid() ? date : null;
} }
return null; return null;