🌐 AI搜索 & 代理 主页
Skip to content

Commit f362802

Browse files
authored
feat(b-calendar, b-form-datepicker): relax YYYY-MM-DD string parsing (closes #5232) (#5242)
* feat(b-calendar, b-form-datepicker): relax date `YYYY-MM-DD` string matching (closes #5232) * Update date.spec.js
1 parent 4b2b7d9 commit f362802

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/utils/date.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { toInteger } from './number'
66

77
// --- Constants ---
88

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/
1015

1116
// --- Date utility methods ---
1217

@@ -16,7 +21,7 @@ export const createDate = (...args) => new Date(...args)
1621
// Parse a date sting, or Date object, into a Date object (with no time information)
1722
export const parseYMD = date => {
1823
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))
2025
return createDate(year, month - 1, day)
2126
} else if (isDate(date)) {
2227
return createDate(date.getFullYear(), date.getMonth(), date.getDate())

src/utils/date.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ describe('utils/date', () => {
1717
it('parseYMD works', async () => {
1818
const date1 = parseYMD('2020-01-15')
1919
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')
2022

2123
expect(date1.toISOString()).toEqual(date2.toISOString())
24+
expect(date1.toISOString()).toEqual(date3.toISOString())
25+
expect(date1.toISOString()).toEqual(date4.toISOString())
2226
expect(parseYMD('yyyy-mm-dd')).toEqual(null)
27+
expect(parseYMD('2020-01-15XYZ')).toEqual(null)
2328
})
2429

2530
it('formatYMD works', async () => {
@@ -28,6 +33,9 @@ describe('utils/date', () => {
2833
expect(formatYMD('2020-01-32')).toEqual('2020-02-01')
2934
expect(formatYMD('adsadsad')).toEqual(null)
3035
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')
3139
})
3240

3341
it('datesEqual works', async () => {

0 commit comments

Comments
 (0)