-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
37 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { helper } from "@ember/component/helper"; | ||
import parseDate from "hermes/utils/parse-date"; | ||
|
||
export default helper(([time]: [string | number | Date | undefined]) => { | ||
return parseDate(time); | ||
}); | ||
export default helper( | ||
([time, monthFormat = "short"]: [ | ||
string | number | Date | undefined, | ||
"short" | "long" | ||
]) => { | ||
return parseDate(time, monthFormat); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
/** | ||
* Converts a valid Date/timestamp into a "MM/DD/YYYY"-formatted string. | ||
* Converts a valid Date/timestamp into a string formatted | ||
* as "DD MMM. YYYY" or "DD MMMM YYYY" (if monthFormat is "long"). | ||
* Returns null if the time parameter is invalid. | ||
*/ | ||
|
||
export default function parseDate( | ||
time?: string | number | Date | ||
time?: string | number | Date, | ||
monthFormat: "short" | "long" = "short" | ||
): string | null { | ||
if (!time) { | ||
return null; | ||
} | ||
const date = new Date(time); | ||
|
||
// `getMonth()` returns a zero-indexed month so we add 1 | ||
let month = date.getMonth() + 1; | ||
if (isNaN(date.getTime())) { | ||
return null; | ||
} | ||
|
||
let day = date.getDate(); | ||
let year = date.getFullYear(); | ||
let month = date.toLocaleString("default", { month: monthFormat }); | ||
|
||
// Nullify invalid timestamps | ||
if (isNaN(month) || isNaN(day) || isNaN(year)) { | ||
return null; | ||
if (monthFormat === "short") { | ||
month += "."; | ||
} | ||
|
||
return `${month}/${day}/${year}`; | ||
return `${day} ${month} ${year}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters