Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datetime): format() options #4285

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datetime/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ interface DateTimeFormatPart {

type TimeZone = "UTC";

interface Options {
export interface Options {
timeZone?: TimeZone;
}

Expand Down
11 changes: 8 additions & 3 deletions datetime/format.ts
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -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);
}
9 changes: 9 additions & 0 deletions datetime/format_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
),
);
},
});
Loading