diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index c7082833c..ae3e4ae7c 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -49,10 +49,13 @@ const meridiemMatch = (input, isLowerCase) => { if (!meridiem) { isAfternoon = input === (isLowerCase ? 'pm' : 'PM') } else { - for (let i = 1; i <= 24; i += 1) { - // todo: fix input === meridiem(i, 0, isLowerCase) + // Iterate over a full 24-hour range so the meridiem string for each hour is + // checked. The previous loop started at hour 1 and used an `i > 12` check, + // but a PM string first matched at i=12 (12 > 12 === false) and was misread + // as AM for non-English locales such as `ko` (오전/오후). (#2031) + for (let i = 0; i <= 23; i += 1) { if (input.indexOf(meridiem(i, 0, isLowerCase)) > -1) { - isAfternoon = i > 12 + isAfternoon = i >= 12 break } } diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index fb4030176..2304f09f4 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -4,6 +4,7 @@ import dayjs from '../../src' import '../../src/locale/ru' import uk from '../../src/locale/uk' import '../../src/locale/zh-cn' +import '../../src/locale/ko' import customParseFormat from '../../src/plugin/customParseFormat' import advancedFormat from '../../src/plugin/advancedFormat' import localizedFormats from '../../src/plugin/localizedFormat' @@ -349,6 +350,26 @@ describe('meridiem locale', () => { }) }) +// issue 2031 +describe('parse localized meridiem (ko) in strict mode', () => { + const format = 'YYYY-MM-DD hh:mm A' + it('AM', () => { + expect(dayjs('2024-01-01 01:00 오전', format, 'ko', true).hour()).toBe(1) + }) + it('PM', () => { + expect(dayjs('2024-01-01 01:00 오후', format, 'ko', true).hour()).toBe(13) + }) + it('noon (12 PM)', () => { + expect(dayjs('2024-01-01 12:00 오후', format, 'ko', true).hour()).toBe(12) + }) + it('midnight (12 AM)', () => { + expect(dayjs('2024-01-01 12:00 오전', format, 'ko', true).hour()).toBe(0) + }) + it('11 PM', () => { + expect(dayjs('2024-01-01 11:00 오후', format, 'ko', true).hour()).toBe(23) + }) +}) + it('parse a string for MMM month format with underscore delimiter', () => { const input = 'Jan_2021' const format = 'MMM_YYYY'