From 96f4a5f3bfea555ed7ec1480f8f9bd1ff9a6e62a Mon Sep 17 00:00:00 2001 From: Mauro de Souza Date: Wed, 1 May 2024 18:00:04 -0300 Subject: [PATCH] feat(time): add meridian template --- src/util/time.ts | 5 ++++- test/ut/spec/util/time.test.ts | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/util/time.ts b/src/util/time.ts index 39b69536db..2e1ba9b423 100644 --- a/src/util/time.ts +++ b/src/util/time.ts @@ -121,7 +121,8 @@ export function format( const m = date[minutesGetterName(isUTC)](); const s = date[secondsGetterName(isUTC)](); const S = date[millisecondsGetterName(isUTC)](); - + const a = H >= 12 ? 'pm' : 'am'; + const A = a.toUpperCase(); const localeModel = lang instanceof Model ? lang : getLocaleModel(lang || SYSTEM_LANG) || getDefaultLocaleModel(); @@ -132,6 +133,8 @@ export function format( const dayOfWeekAbbr = timeModel.get('dayOfWeekAbbr'); return (template || '') + .replace(/{a}/g, a + '') + .replace(/{A}/g, A + '') .replace(/{yyyy}/g, y + '') .replace(/{yy}/g, pad(y % 100 + '', 2)) .replace(/{Q}/g, q + '') diff --git a/test/ut/spec/util/time.test.ts b/test/ut/spec/util/time.test.ts index ac459764b7..db5aefcdfe 100755 --- a/test/ut/spec/util/time.test.ts +++ b/test/ut/spec/util/time.test.ts @@ -29,6 +29,7 @@ describe('util/time', function () { const time = new Date('2003-04-09 01:04:02.300 UTC'); const anotherTime = new Date('2023-12-19 11:44:33.003 UTC'); + const oneMoreTime = new Date('2023-01-12 13:09:01.035 UTC'); // test {yyyy}, {yy} ... it('should format year', function () { @@ -127,5 +128,18 @@ describe('util/time', function () { expect(format(time, '{S}', true)).toEqual('300'); expect(format(anotherTime, '{S}', true)).toEqual('3'); }); + + // test {a} ... + it('should format meridian', function () { + expect(format(time, '{a}', true)).toEqual('am'); + expect(format(anotherTime, '{a}', true)).toEqual('am'); + expect(format(oneMoreTime, '{a}', true)).toEqual('pm'); + }); + + it('should format meridian in uppercase', function () { + expect(format(time, '{A}', true)).toEqual('AM'); + expect(format(anotherTime, '{A}', true)).toEqual('AM'); + expect(format(oneMoreTime, '{A}', true)).toEqual('PM'); + }); }); -}); \ No newline at end of file +});