(dateString?: string, includeTime = true)
| 8 | * @throws - Error when the date cannot be parsed from the string |
| 9 | */ |
| 10 | export default function parseDateString(dateString?: string, includeTime = true): DateObject { |
| 11 | if (!dateString) { |
| 12 | return { |
| 13 | year: -1, |
| 14 | month: -1, |
| 15 | day: -1, |
| 16 | hour: includeTime ? -1 : 0, |
| 17 | minute: includeTime ? -1 : 0, |
| 18 | second: includeTime ? -1 : 0, |
| 19 | }; |
| 20 | } |
| 21 | const date = new Date(dateString); |
| 22 | if (Number.isNaN(date.getTime())) { |
| 23 | throw new Error(`Unable to parse date ${dateString}`); |
| 24 | } |
| 25 | return { |
| 26 | year: date.getUTCFullYear(), |
| 27 | month: date.getUTCMonth() + 1, // oh you, javascript. |
| 28 | day: date.getUTCDate(), |
| 29 | hour: includeTime ? date.getUTCHours() : 0, |
| 30 | minute: includeTime ? date.getUTCMinutes() : 0, |
| 31 | second: includeTime ? date.getUTCSeconds() : 0, |
| 32 | }; |
| 33 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…