Skip to content

Commit a4e234e

Browse files
committed
feat(util): add timeZone option
1 parent 85fcde3 commit a4e234e

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

__tests__/unit/utils/time.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ test("splittedBy", () => {
1515
expect(result[1]).toEqual("02");
1616
// .toEqual(['2022', '02', '06']) NOTE: local -> JST, CI -> UTC
1717
});
18+
19+
test("splittedBy: with timezone", () => {
20+
// 2022-02-05T15:33:26Z is 2022-02-06 00:33 in JST
21+
expect(splittedBy(1644075206, "ja-JP", "/", "Asia/Tokyo")).toEqual([
22+
"2022",
23+
"02",
24+
"06"
25+
]);
26+
expect(splittedBy(1644075206, "ja-JP", "/", "UTC")).toEqual([
27+
"2022",
28+
"02",
29+
"05"
30+
]);
31+
});

src/utils/time.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ export function toISODateString(unixTime: number): string {
2525
export function splittedBy(
2626
unixTime: number,
2727
locale: string,
28-
delimiter: string
28+
delimiter: string,
29+
timeZone?: string
2930
): Array<string> {
3031
return toDate(unixTime)
3132
.toLocaleDateString(locale, {
3233
year: "numeric",
3334
month: "2-digit",
34-
day: "2-digit"
35+
day: "2-digit",
36+
...(timeZone ? { timeZone } : {})
3537
})
3638
.split(delimiter);
3739
}

0 commit comments

Comments
 (0)