From d9906cc0ccf347bfecc738e452e4e05ba62e6562 Mon Sep 17 00:00:00 2001 From: babiabeo Date: Wed, 7 Feb 2024 21:49:49 +0700 Subject: [PATCH 1/3] fix(datetime): allow passing options to `format()` --- datetime/_common.ts | 2 +- datetime/format.ts | 11 ++++++++--- datetime/format_test.ts | 9 +++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/datetime/_common.ts b/datetime/_common.ts index 12d011f2c324..28992106bcd2 100644 --- a/datetime/_common.ts +++ b/datetime/_common.ts @@ -102,7 +102,7 @@ interface DateTimeFormatPart { type TimeZone = "UTC"; -interface Options { +export interface Options { timeZone?: TimeZone; } diff --git a/datetime/format.ts b/datetime/format.ts index 09cc68d4c15b..17fa8678e270 100644 --- a/datetime/format.ts +++ b/datetime/format.ts @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. -import { DateTimeFormatter } from "./_common.ts"; +import { DateTimeFormatter, Options } from "./_common.ts"; /** * Takes an input `date` and a `formatString` to format to a `string`. @@ -22,9 +22,14 @@ import { DateTimeFormatter } from "./_common.ts"; * * @param date Date * @param formatString Format string + * @param options Format options * @return formatted date string */ -export function format(date: Date, formatString: string): string { +export function format( + date: Date, + formatString: string, + options?: Options, +): string { const formatter = new DateTimeFormatter(formatString); - return formatter.format(date); + return formatter.format(date, options); } diff --git a/datetime/format_test.ts b/datetime/format_test.ts index 0023ca89e1f1..88f4b838fbf5 100644 --- a/datetime/format_test.ts +++ b/datetime/format_test.ts @@ -102,5 +102,14 @@ Deno.test({ "1", format(new Date("2019-01-01T13:00:00.000"), "h"), ); + + assertEquals( + "2019-01-01 04:00:00.000", + format( + new Date("2019-01-01T13:00:00.000+09:00"), + "yyyy-MM-dd HH:mm:ss.SSS", + { timeZone: "UTC" }, + ), + ); }, }); From c0d0c2bf887bf6d4136a3330b1b1c1ea0ff8b9a2 Mon Sep 17 00:00:00 2001 From: babiabeo Date: Thu, 8 Feb 2024 07:16:54 +0700 Subject: [PATCH 2/3] new solution --- datetime/_common.ts | 2 +- datetime/format.ts | 25 ++++++++++++++++++------- datetime/format_test.ts | 11 ++++++++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/datetime/_common.ts b/datetime/_common.ts index 28992106bcd2..12d011f2c324 100644 --- a/datetime/_common.ts +++ b/datetime/_common.ts @@ -102,7 +102,7 @@ interface DateTimeFormatPart { type TimeZone = "UTC"; -export interface Options { +interface Options { timeZone?: TimeZone; } diff --git a/datetime/format.ts b/datetime/format.ts index 17fa8678e270..31954914ac31 100644 --- a/datetime/format.ts +++ b/datetime/format.ts @@ -1,7 +1,7 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. -import { DateTimeFormatter, Options } from "./_common.ts"; +import { DateTimeFormatter } from "./_common.ts"; /** * Takes an input `date` and a `formatString` to format to a `string`. @@ -18,18 +18,29 @@ import { DateTimeFormatter, Options } from "./_common.ts"; * format(new Date(2019, 0, 20, 16, 34), "HH:mm MM-dd-yyyy"); // output : "16:34 01-20-2019" * format(new Date(2019, 0, 20, 16, 34, 23, 123), "MM-dd-yyyy HH:mm:ss.SSS"); // output : "01-20-2019 16:34:23.123" * format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd"); // output : "today: 2019-01-20" + * format(new Date("2019-01-20T16:34:23:123-05:00"), "yyyy-MM-dd HH:mm:ss", { utc: true }); + * // output : "2019-01-20 21:34:23" * ``` * - * @param date Date - * @param formatString Format string - * @param options Format options - * @return formatted date string + * @param date The date to be formatted. + * @param formatString The date time string format. + * @param options The options to customize the formatting of the date. + * @return The formatted date string. */ export function format( date: Date, formatString: string, - options?: Options, + options: FormatOptions = {}, ): string { const formatter = new DateTimeFormatter(formatString); - return formatter.format(date, options); + return formatter.format( + date, + options.utc ? { timeZone: "UTC" } : undefined, + ); +} + +/** The options used for formatting date in {@linkcode format}. */ +export interface FormatOptions { + /** Whether returns the formatted date in UTC instead of local time. */ + utc?: boolean; } diff --git a/datetime/format_test.ts b/datetime/format_test.ts index 88f4b838fbf5..b4acdb3a55d9 100644 --- a/datetime/format_test.ts +++ b/datetime/format_test.ts @@ -108,7 +108,16 @@ Deno.test({ format( new Date("2019-01-01T13:00:00.000+09:00"), "yyyy-MM-dd HH:mm:ss.SSS", - { timeZone: "UTC" }, + { utc: true }, + ), + ); + + assertEquals( + "2019-01-01 18:00:00.000", + format( + new Date("2019-01-01T13:00:00.000-05:00"), + "yyyy-MM-dd HH:mm:ss.SSS", + { utc: true }, ), ); }, From 99869ac78cb1441c8aa1ad7680168a579cee5e34 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 9 Feb 2024 11:34:49 +1100 Subject: [PATCH 3/3] Update datetime/format.ts --- datetime/format.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datetime/format.ts b/datetime/format.ts index 31954914ac31..f4e8b40d30e4 100644 --- a/datetime/format.ts +++ b/datetime/format.ts @@ -39,7 +39,7 @@ export function format( ); } -/** The options used for formatting date in {@linkcode format}. */ +/** Options for {@linkcode format}. */ export interface FormatOptions { /** Whether returns the formatted date in UTC instead of local time. */ utc?: boolean;