diff --git a/npm/ng-packs/packages/core/src/lib/tests/date-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/date-utils.spec.ts new file mode 100644 index 0000000000..2fe14b1e9b --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/date-utils.spec.ts @@ -0,0 +1,39 @@ +import { ConfigStateService, getShortDateFormat, getShortDateShortTimeFormat } from '@abp/ng.core'; + +const dateTimeFormat = { + calendarAlgorithmType: 'SolarCalendar', + dateSeparator: '/', + dateTimeFormatLong: 'dddd, MMMM d, yyyy', + fullDateTimePattern: 'dddd, MMMM d, yyyy h:mm:ss tt', + longTimePattern: 'h:mm:ss tt', + shortDatePattern: 'M/d/yyyy', + shortTimePattern: 'h:mm tt', +}; + +describe('Date Utils', () => { + let config: ConfigStateService; + + beforeEach(() => { + config = new ConfigStateService(undefined); + }); + + describe('#getShortDateFormat', () => { + test('should get the short date format from ConfigState and return it', () => { + const getDeepSpy = jest.spyOn(config, 'getDeep'); + getDeepSpy.mockReturnValueOnce(dateTimeFormat); + + expect(getShortDateFormat(config)).toBe('M/d/yyyy'); + expect(getDeepSpy).toHaveBeenCalledWith('localization.currentCulture.dateTimeFormat'); + }); + }); + + describe('#getShortDateShortTimeFormat', () => { + test('should get the short date time format from ConfigState and return it', () => { + const getDeepSpy = jest.spyOn(config, 'getDeep'); + getDeepSpy.mockReturnValueOnce(dateTimeFormat); + + expect(getShortDateShortTimeFormat(config)).toBe('M/d/yyyy h:mm a'); + expect(getDeepSpy).toHaveBeenCalledWith('localization.currentCulture.dateTimeFormat'); + }); + }); +}); diff --git a/npm/ng-packs/packages/core/src/lib/utils/date-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/date-utils.ts new file mode 100644 index 0000000000..321af986da --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/utils/date-utils.ts @@ -0,0 +1,18 @@ +import { ApplicationConfiguration } from '../models/application-configuration'; +import { ConfigStateService } from '../services'; + +export function getShortDateFormat(configStateService: ConfigStateService) { + const dateTimeFormat = configStateService.getDeep( + 'localization.currentCulture.dateTimeFormat', + ) as ApplicationConfiguration.DateTimeFormat; + + return dateTimeFormat.shortDatePattern; +} + +export function getShortDateShortTimeFormat(configStateService: ConfigStateService) { + const dateTimeFormat = configStateService.getDeep( + 'localization.currentCulture.dateTimeFormat', + ) as ApplicationConfiguration.DateTimeFormat; + + return `${dateTimeFormat.shortDatePattern} ${dateTimeFormat.shortTimePattern.replace('tt', 'a')}`; +} diff --git a/npm/ng-packs/packages/core/src/lib/utils/index.ts b/npm/ng-packs/packages/core/src/lib/utils/index.ts index 48efb760e9..87b84ef800 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/index.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/index.ts @@ -1,5 +1,6 @@ export * from './array-utils'; export * from './common-utils'; +export * from './date-utils'; export * from './environment-utils'; export * from './factory-utils'; export * from './form-utils';