Skip to content

Commit 07d1a5c

Browse files
committed
Allow time to be excluded when calling formatDateTime
1 parent 071a437 commit 07d1a5c

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

src/util/date-and-time.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ export type FormatDateTimeOptions = {
247247
*/
248248
includeWeekday?: boolean;
249249

250-
/** Test seam. JS `Intl` API implementation. */
251-
Intl?: IntlType;
250+
/**
251+
* Whether the formatted date should include the time (hours and minutes) or not.
252+
* Defaults to `true`.
253+
*/
254+
includeTime?: boolean;
252255
};
253256

254257
/**
@@ -261,18 +264,20 @@ export type FormatDateTimeOptions = {
261264
*/
262265
export function formatDateTime(
263266
date: Date | string,
264-
options?: FormatDateTimeOptions,
267+
{ includeWeekday = false, includeTime = true }: FormatDateTimeOptions = {},
268+
/* istanbul ignore next - Test seam. JS `Intl` API implementation. */
269+
Intl?: IntlType,
265270
): string {
266271
return format(
267272
typeof date === 'string' ? new Date(date) : date,
268273
{
269274
year: 'numeric',
270275
month: 'short',
271276
day: '2-digit',
272-
weekday: options?.includeWeekday ? 'long' : undefined,
273-
hour: '2-digit',
274-
minute: '2-digit',
277+
weekday: includeWeekday ? 'long' : undefined,
278+
hour: includeTime ? '2-digit' : undefined,
279+
minute: includeTime ? '2-digit' : undefined,
275280
},
276-
options?.Intl,
281+
Intl,
277282
);
278283
}

src/util/test/date-and-time-test.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,28 +238,63 @@ describe('date-and-time', () => {
238238
{
239239
locale: 'en-US',
240240
date: new Date('2020-05-04T23:02:01'),
241-
includeWeekday: true,
241+
expectedResult: 'May 04, 2020, 11:02 PM',
242+
},
243+
{
244+
locale: 'en-US',
245+
date: new Date('2020-05-04T23:02:01'),
246+
options: { includeWeekday: true },
242247
expectedResult: 'Monday, May 04, 2020, 11:02 PM',
243248
},
244249
{
245250
locale: 'de-DE',
246251
date: new Date('2020-05-04T23:02:01'),
247-
includeWeekday: true,
252+
options: { includeWeekday: true },
248253
expectedResult: 'Montag, 04. Mai 2020, 23:02',
249254
},
250255
{
251256
locale: 'en-US',
252257
date: '2020-05-04T23:02:01',
253-
includeWeekday: false,
258+
options: { includeWeekday: false },
254259
expectedResult: 'May 04, 2020, 11:02 PM',
255260
},
256261
{
257262
locale: 'de-DE',
258263
date: '2020-05-04T23:02:01',
259-
includeWeekday: false,
264+
options: { includeWeekday: false },
260265
expectedResult: '04. Mai 2020, 23:02',
261266
},
262-
].forEach(({ locale, includeWeekday, expectedResult, date }) => {
267+
{
268+
locale: 'de-DE',
269+
date: '2020-05-04T23:02:01',
270+
options: { includeTime: false },
271+
expectedResult: '04. Mai 2020',
272+
},
273+
{
274+
locale: 'en-US',
275+
date: '2020-05-04T23:02:01',
276+
options: { includeTime: false },
277+
expectedResult: 'May 04, 2020',
278+
},
279+
{
280+
locale: 'de-DE',
281+
date: '2020-05-04T23:02:01',
282+
options: {
283+
includeTime: false,
284+
includeWeekday: true,
285+
},
286+
expectedResult: 'Montag, 04. Mai 2020',
287+
},
288+
{
289+
locale: 'en-US',
290+
date: '2020-05-04T23:02:01',
291+
options: {
292+
includeTime: false,
293+
includeWeekday: true,
294+
},
295+
expectedResult: 'Monday, May 04, 2020',
296+
},
297+
].forEach(({ locale, options, expectedResult, date }) => {
263298
it('returns absolute formatted date', () => {
264299
const fakeIntl = locale => ({
265300
DateTimeFormat: function (_, options) {
@@ -268,9 +303,7 @@ describe('date-and-time', () => {
268303
});
269304

270305
assert.equal(
271-
normalizeSpaces(
272-
formatDateTime(date, { Intl: fakeIntl(locale), includeWeekday }),
273-
),
306+
normalizeSpaces(formatDateTime(date, options, fakeIntl(locale))),
274307
expectedResult,
275308
);
276309
});

0 commit comments

Comments
 (0)