UNPKG

date-vir

Version:

Easy and explicit dates and times.

36 lines (35 loc) 951 B
import { DateTime } from 'luxon'; /** * These formats are for luxon, documented here: * https://moment.github.io/luxon/#/parsing?id=table-of-tokens */ const knownStringFormats = [ // 1-2024 'L-y', // jan-2021 'LLL-y', // january-2021 'LLLL-y', ]; /** * Tries extra known formats for parsing date strings, even those that browsers do not natively * support. * * @category Internal */ export function parseDateString(dateString, timezone) { const isoAttempt = DateTime.fromISO(dateString, { zone: timezone }); if (isoAttempt.isValid) { return isoAttempt; } let finalizedDateTime; knownStringFormats.some((format) => { const dateTimeAttempt = DateTime.fromFormat(dateString, format, { zone: timezone }); if (!dateTimeAttempt.isValid) { return false; } finalizedDateTime = dateTimeAttempt; return true; }); return finalizedDateTime; }