mirror of
https://github.com/TriliumNext/Notes.git
synced 2026-01-06 12:59:55 -06:00
client: date formatter utility: add the ability to format exclusively dates or times
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
/**
|
||||
* Formats the given date to a string based on the current locale.
|
||||
* @param {Date | number} date
|
||||
* @param {"full" | "long" | "medium" | "short" | undefined} dateStyle
|
||||
* @param {"full" | "long" | "medium" | "short" | undefined} tiemStyle
|
||||
* @param {"full" | "long" | "medium" | "short" | "none" | undefined} dateStyle
|
||||
* @param {"full" | "long" | "medium" | "short" | "none" | undefined} tiemStyle
|
||||
*/
|
||||
export function formatDate(date, dateStyle = "medium", tiemStyle = "medium") {
|
||||
const formatter = new Intl.DateTimeFormat(navigator.language, {
|
||||
dateStyle: "medium",
|
||||
timeStyle: "medium"
|
||||
});
|
||||
export function formatDate(date, dateStyle = "medium", timeStyle = "medium") {
|
||||
const locale = navigator.language;
|
||||
|
||||
if (timeStyle === "none") {
|
||||
// Format only the date
|
||||
return date.toLocaleDateString(locale, {dateStyle});
|
||||
} else if (dateStyle === "none") {
|
||||
// Format only the time
|
||||
return date.toLocaleTimeString(locale, {timeStyle});
|
||||
} else {
|
||||
// Format the date and time
|
||||
const formatter = new Intl.DateTimeFormat(navigator.language, {dateStyle, timeStyle});
|
||||
return formatter.format(date);
|
||||
}
|
||||
}
|
||||
|
||||
return formatter.format(date);
|
||||
}
|
||||
Reference in New Issue
Block a user