Skip to content

Commit 361fe13

Browse files
feat(util): formatLocalToZonedDate helper
rename corresponding helper to formatZonedToLocalDate
1 parent 1d07859 commit 361fe13

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

packages/frontend/lib/filters/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export {
22
excerpt,
33
formatDate as date,
4-
formatDateToLocal as localDate,
4+
formatZonedToLocalDate as localDate,
55
getCanonicalUrl as url,
66
slugify,
77
} from "@indiekit/util";

packages/util/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export { getCursor, getObjectId } from "./lib/collection.js";
22
export {
33
dateTokens,
44
formatDate,
5-
formatDateToLocal,
5+
formatZonedToLocalDate,
6+
formatLocalToZonedDate,
67
getDate,
78
getTimeZoneDesignator,
89
getTimeZoneOffset,

packages/util/lib/date.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,34 @@ export const formatDate = (string, tokens, options = {}) => {
5353
};
5454

5555
/**
56-
* Format a date as local date
56+
* Formats zoned date as local date
5757
* Used to convert date to value consumable by input[type="datetime-local"]
5858
* @param {Date|string|number} string - Zoned date, i.e. 2023-08-28T12:30+01:00
5959
* @param {string} timeZone - Time zone
6060
* @returns {string} Formatted local date, i.e. 2023-08-28T12:30
6161
*/
62-
export const formatDateToLocal = (string, timeZone) => {
62+
export const formatZonedToLocalDate = (string, timeZone) => {
6363
const dateTime = format(string, "yyyy-MM-dd'T'HH:mm", {
6464
in: tz(timeZone),
6565
});
6666

6767
return dateTime;
6868
};
6969

70+
/**
71+
* Format local date to zoned date
72+
* @param {Date|string|number} string - Local date, i.e. 2023-08-28T12:30
73+
* @param {string} timeZone - Time zone
74+
* @returns {string} Zoned date, i.e. 2023-08-28T12:30+01:00
75+
*/
76+
export const formatLocalToZonedDate = (string, timeZone) => {
77+
const formattedDateTime = format(string, "XXX", {
78+
in: tz(timeZone),
79+
});
80+
81+
return `${string}${formattedDateTime}`;
82+
};
83+
7084
/**
7185
* Converts date to use configured time zone
7286
* @param {string} setting - Time zone setting

packages/util/test/unit/date.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { isValid, parseISO } from "date-fns";
55

66
import {
77
formatDate,
8-
formatDateToLocal,
8+
formatZonedToLocalDate,
9+
formatLocalToZonedDate,
910
getDate,
1011
getTimeZoneDesignator,
1112
getTimeZoneOffset,
@@ -36,19 +37,32 @@ describe("util/lib/date", () => {
3637
assert.equal(result, now);
3738
});
3839

39-
it("Formats a date as local date", () => {
40-
const tz1 = formatDateToLocal("2019-11-30T12:30:00+01:00", "Asia/Taipei");
41-
const tz2 = formatDateToLocal(
40+
it("Formats zoned date as local date", () => {
41+
const tz1 = formatZonedToLocalDate(
42+
"2019-11-30T12:30:00+01:00",
43+
"Asia/Taipei",
44+
);
45+
const tz2 = formatZonedToLocalDate(
4246
"2019-11-30T12:30:00+01:00",
4347
"America/Panama",
4448
);
45-
const utc = formatDateToLocal("2019-11-30T12:30:00+01:00", "UTC");
49+
const utc = formatZonedToLocalDate("2019-11-30T12:30:00+01:00", "UTC");
4650

4751
assert.equal(tz1, "2019-11-30T19:30");
4852
assert.equal(tz2, "2019-11-30T06:30");
4953
assert.equal(utc, "2019-11-30T11:30");
5054
});
5155

56+
it("Formats local date to zoned date", () => {
57+
const tz1 = formatLocalToZonedDate("2019-11-30T12:30:00", "Asia/Taipei");
58+
const tz2 = formatLocalToZonedDate("2019-11-30T12:30:00", "America/Panama");
59+
const utc = formatLocalToZonedDate("2019-11-30T12:30:00", "UTC");
60+
61+
assert.equal(tz1, "2019-11-30T12:30:00+08:00");
62+
assert.equal(tz2, "2019-11-30T12:30:00-05:00");
63+
assert.equal(utc, "2019-11-30T12:30:00Z");
64+
});
65+
5266
it("Creates UTC datetime from `client`", () => {
5367
const result = getDate("client");
5468

0 commit comments

Comments
 (0)