-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseDate.js
57 lines (51 loc) · 2.07 KB
/
parseDate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { parse } from 'date-fns/parse'
import { parseISO } from 'date-fns/parseISO'
import { getTimezoneOffset } from 'date-fns-tz/getTimezoneOffset'
import { addMinutes } from 'date-fns/addMinutes'
import { addMilliseconds } from 'date-fns/addMilliseconds'
import isValidDate from './isValidDate.js'
/**
* Parses a given input into a date object, handling different date formats and timezones.
*
* @param {any} rawInput - The date input to parse. Can be a date string or a date object.
* @param {string} [tz] - The timezone to use for parsing. Only relevant for 'yyyy-MM-dd' date strings. If not provided, uses the system's timezone.
* @return {Date|null} - Returns a Date object if parsing is successful; otherwise, returns null.
*
* @example
* // Basic parsing with system timezone
* parseDate('2023-01-01')
*
* @example
* // Parsing with a specific timezone
* parseDate('2023-01-01', 'Europe/Berlin')
*
* @example
* // Parsing a full datetime string
* parseDate('2023-01-01T12:00:00')
*/
export default function parseDate (rawInput, tz = undefined) {
if (typeof rawInput === 'boolean') return null
if ([null, undefined].includes(rawInput)) return null
if (isValidDate(rawInput)) return new Date(rawInput)
if (typeof rawInput === 'string') {
// Test for yyyy-MM-dd
const regex1 = /^(\d{4})-(\d{2})-(\d{2})$/
if (regex1.test(rawInput)) {
const inSystemZone = parse(rawInput, 'yyyy-MM-dd', new Date())
if (!tz) return inSystemZone
const inUTC = addMinutes(inSystemZone, -inSystemZone.getTimezoneOffset())
const inOutZone = addMilliseconds(inUTC, getTimezoneOffset(tz, inUTC))
return inOutZone
}
// Test for yyyy-MM-ddTHH:mm:ss
const regex2 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/
if (regex2.test(rawInput)) return parse(rawInput, 'yyyy-MM-dd\'T\'HH:mm:ss', new Date())
}
const parsedNative = new Date(rawInput)
if (isValidDate(parsedNative)) return parsedNative
if (typeof rawInput === 'string') {
const parsedISO = parseISO(rawInput)
if (isValidDate(parsedISO)) return parsedISO
}
return null
}