Skip to content

Commit a5322ed

Browse files
authored
Merge pull request #43 from nshiab/42-month-day-format-for-dates
adding Month options to formatDateLocal and formatData
2 parents 41fd4d3 + cbba795 commit a5322ed

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/format/formatDate.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default function formatDate(
1919
| "YYYY-MM-DD"
2020
| "Month Day, YYYY"
2121
| "Month Day, YYYY, at HH:MM period"
22-
| "DayName",
22+
| "DayName"
23+
| "Month",
2324
options: {
2425
style?: "cbc" | "rc"
2526
abbreviations?: boolean
@@ -57,6 +58,8 @@ export default function formatDate(
5758
dateFormatted = dateToString(date, representations[mergedOptions.style])
5859
} else if (format === "DayName") {
5960
dateFormatted = dateToString(date, "%A")
61+
} else if (format === "Month") {
62+
dateFormatted = dateToString(date, "%B")
6063
} else {
6164
throw new Error("Unknown format")
6265
}

src/format/formatDateLocal.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default function formatDateLocal(
1919
| "YYYY-MM-DD"
2020
| "Month Day, YYYY"
2121
| "Month Day, YYYY, at HH:MM period"
22-
| "DayName",
22+
| "DayName"
23+
| "Month",
2324
options: {
2425
style?: "cbc" | "rc"
2526
abbreviations?: boolean
@@ -57,6 +58,8 @@ export default function formatDateLocal(
5758
dateFormatted = dateToString(date, representations[mergedOptions.style])
5859
} else if (format === "DayName") {
5960
dateFormatted = dateToString(date, "%A")
61+
} else if (format === "Month") {
62+
dateFormatted = dateToString(date, "%B")
6063
} else {
6164
throw new Error("Unknown format")
6265
}

test/format/formatDate.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ describe("formatDate", () => {
5656
})
5757
assert.strictEqual(formattedDate, "Sun.")
5858
})
59+
it("should return the full month name", () => {
60+
const formattedDate = formatDate(date, "Month")
61+
assert.strictEqual(formattedDate, "January")
62+
})
63+
it("should return the abbreviated month name", () => {
64+
const formattedDate = formatDate(date, "Month", {
65+
abbreviations: true,
66+
})
67+
assert.strictEqual(formattedDate, "Jan.")
68+
})
5969

6070
// Radio-Canada style
6171

@@ -111,4 +121,17 @@ describe("formatDate", () => {
111121
})
112122
assert.strictEqual(formattedDate, "Dim.")
113123
})
124+
it("should return the full month name with RC style", () => {
125+
const formattedDate = formatDate(date, "Month", {
126+
style: "rc",
127+
})
128+
assert.strictEqual(formattedDate, "janvier")
129+
})
130+
it("should return the abbreviated month name with RC style", () => {
131+
const formattedDate = formatDate(dateNoMinutes, "Month", {
132+
style: "rc",
133+
abbreviations: true,
134+
})
135+
assert.strictEqual(formattedDate, "janv.")
136+
})
114137
})

test/format/formatDateLocal.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ describe("formatDateLocal", () => {
6868
})
6969
assert.strictEqual(formattedDate, "Sun.")
7070
})
71+
it("should return the full month name", () => {
72+
const formattedDate = formatDateLocal(date, "Month")
73+
assert.strictEqual(formattedDate, "January")
74+
})
75+
it("should return the abbreviated month name", () => {
76+
const formattedDate = formatDateLocal(date, "Month", {
77+
abbreviations: true,
78+
})
79+
assert.strictEqual(formattedDate, "Jan.")
80+
})
7181

7282
// Radio-Canada style
7383

@@ -131,4 +141,17 @@ describe("formatDateLocal", () => {
131141
})
132142
assert.strictEqual(formattedDate, "Dim.")
133143
})
144+
it("should return the full month name with RC style", () => {
145+
const formattedDate = formatDateLocal(date, "Month", {
146+
style: "rc",
147+
})
148+
assert.strictEqual(formattedDate, "janvier")
149+
})
150+
it("should return the abbreviated month name with RC style", () => {
151+
const formattedDate = formatDateLocal(dateNoMinutes, "Month", {
152+
style: "rc",
153+
abbreviations: true,
154+
})
155+
assert.strictEqual(formattedDate, "janv.")
156+
})
134157
})

0 commit comments

Comments
 (0)