Skip to content

Commit 16af3b9

Browse files
feat(util): enable date formatter to accept upstream timeZone option
1 parent 9f4810c commit 16af3b9

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

packages/util/lib/date.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@ export const dateTokens = [
3636
* Format a date
3737
* @param {string} string - ISO 8601 date
3838
* @param {string} tokens - Tokenised date format
39-
* @param {string} [locale] - Locale name
40-
* @param {import("date-fns-tz").OptionsWithTZ} [options] - Options
39+
* @param {object} [options] - Options
40+
* @param {string} [options.locale] - Locale
41+
* @param {string} [options.timeZone] - Time zone
4142
* @returns {string} Formatted date
4243
*/
43-
export const formatDate = (string, tokens, locale, options = {}) => {
44-
locale = String(locale || "en").replace("-", "");
45-
options.locale = locales[locale];
44+
export const formatDate = (string, tokens, options = {}) => {
45+
const formattedLocale = String(options.locale || "en").replace("-", "");
46+
47+
// Convert options object to options expected for `FormatOptionsWithTZ`
48+
const formatOptions = {
49+
...options,
50+
...(options.locale && { locale: locales[formattedLocale] }),
51+
};
4652

4753
const date = string === "now" ? new Date() : parseISO(string);
48-
const dateTime = format(date, tokens, options);
54+
const dateTime = format(date, tokens, formatOptions);
4955
return dateTime;
5056
};
5157

packages/util/test/unit/date.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ import {
1313
describe("util/lib/date", () => {
1414
it("Formats a date", () => {
1515
assert.equal(formatDate("2019-11-30", "dd MMMM yyyy"), "30 November 2019");
16-
assert.equal(formatDate("2019-11-30", "PPP", "en-GB"), "30 November 2019");
1716
assert.equal(
18-
formatDate("2019-11-30", "PPP", "en-US"),
17+
formatDate("2019-11-30", "PPP", { locale: "en-GB" }),
18+
"30 November 2019",
19+
);
20+
assert.equal(
21+
formatDate("2019-11-30", "PPP", { locale: "en-US" }),
1922
"November 30th, 2019",
2023
);
2124
assert.equal(
22-
formatDate("2019-11-30", "PPP", "pt-BR"),
25+
formatDate("2019-11-30", "PPP", { locale: "pt-BR" }),
2326
"30 de novembro de 2019",
2427
);
2528
});

0 commit comments

Comments
 (0)