+15
-2
lines changedFilter options
+15
-2
lines changed Original file line number Diff line number Diff line change
@@ -6,7 +6,12 @@ import { toInteger } from './number'
6
6
7
7
// --- Constants ---
8
8
9
-
const RX_DATE = /^\d+-\d+-\d+$/
9
+
// Loose YYYY-MM-DD matching, ignores any appended time inforation
10
+
// Matches '1999-12-20', '1999-1-1', '1999-01-20T22:51:49.118Z', '1999-01-02 13:00:00'
11
+
const RX_DATE = /^\d+-\d\d?-\d\d?(?:\s|T|$)/
12
+
13
+
// Used to split off the date parts of the YYYY-MM-DD string
14
+
const RX_DATE_SPLIT = /-|\s|T/
10
15
11
16
// --- Date utility methods ---
12
17
@@ -16,7 +21,7 @@ export const createDate = (...args) => new Date(...args)
16
21
// Parse a date sting, or Date object, into a Date object (with no time information)
17
22
export const parseYMD = date => {
18
23
if (isString(date) && RX_DATE.test(date.trim())) {
19
-
const [year, month, day] = date.split('-').map(toInteger)
24
+
const [year, month, day] = date.split(RX_DATE_SPLIT).map(v => toInteger(v, 1))
20
25
return createDate(year, month - 1, day)
21
26
} else if (isDate(date)) {
22
27
return createDate(date.getFullYear(), date.getMonth(), date.getDate())
Original file line number Diff line number Diff line change
@@ -17,9 +17,14 @@ describe('utils/date', () => {
17
17
it('parseYMD works', async () => {
18
18
const date1 = parseYMD('2020-01-15')
19
19
const date2 = new Date(2020, 0, 15)
20
+
const date3 = parseYMD('2020-01-15T23:16:56.131Z')
21
+
const date4 = parseYMD('2020-01-15 23:16:56')
20
22
21
23
expect(date1.toISOString()).toEqual(date2.toISOString())
24
+
expect(date1.toISOString()).toEqual(date3.toISOString())
25
+
expect(date1.toISOString()).toEqual(date4.toISOString())
22
26
expect(parseYMD('yyyy-mm-dd')).toEqual(null)
27
+
expect(parseYMD('2020-01-15XYZ')).toEqual(null)
23
28
})
24
29
25
30
it('formatYMD works', async () => {
@@ -28,6 +33,9 @@ describe('utils/date', () => {
28
33
expect(formatYMD('2020-01-32')).toEqual('2020-02-01')
29
34
expect(formatYMD('adsadsad')).toEqual(null)
30
35
expect(formatYMD('x2020-01-15')).toEqual(null)
36
+
expect(formatYMD('2020-01-15x')).toEqual(null)
37
+
expect(formatYMD('2020-01-15T23:16:56.131Z')).toEqual('2020-01-15')
38
+
expect(formatYMD('2020-01-15 23:16:56')).toEqual('2020-01-15')
31
39
})
32
40
33
41
it('datesEqual works', async () => {
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4