mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: tweak datetime.ts
This commit is contained in:
@ -42,9 +42,9 @@ export function getTimeString(t?: Date | number | string): string {
|
|||||||
* - "pl" locale: "30.01.2023, 22:05:00"
|
* - "pl" locale: "30.01.2023, 22:05:00"
|
||||||
*/
|
*/
|
||||||
export function getDateTimeString(t?: Date | number | string | any, locale = i18n.language): string {
|
export function getDateTimeString(t?: Date | number | string | any, locale = i18n.language): string {
|
||||||
const tsFromDate = getTimeStampByDate(t ? t : Date.now());
|
const tsFromDate = new Date(getTimeStampByDate(t ? t : Date.now()));
|
||||||
|
try {
|
||||||
return new Date(tsFromDate).toLocaleDateString(locale, {
|
return tsFromDate.toLocaleString(locale, {
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
month: "numeric",
|
month: "numeric",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
@ -52,28 +52,9 @@ export function getDateTimeString(t?: Date | number | string | any, locale = i18
|
|||||||
minute: "numeric",
|
minute: "numeric",
|
||||||
second: "numeric",
|
second: "numeric",
|
||||||
});
|
});
|
||||||
}
|
} catch (error) {
|
||||||
|
return tsFromDate.toLocaleString();
|
||||||
/**
|
}
|
||||||
* Get a localized date string to provided time.
|
|
||||||
*
|
|
||||||
* If no date is provided, the current date is used.
|
|
||||||
*
|
|
||||||
* Note: This function does not include time information.
|
|
||||||
*
|
|
||||||
* Sample outputs:
|
|
||||||
* - "en" locale: "1/30/2023"
|
|
||||||
* - "pt-BR" locale: "30/01/2023"
|
|
||||||
* - "pl" locale: "30.01.2023"
|
|
||||||
*/
|
|
||||||
export function getDateString(t?: Date | number | string, locale = i18n.language): string {
|
|
||||||
const tsFromDate = getTimeStampByDate(t ? t : Date.now());
|
|
||||||
|
|
||||||
return new Date(tsFromDate).toLocaleDateString(locale, {
|
|
||||||
year: "numeric",
|
|
||||||
month: "numeric",
|
|
||||||
day: "numeric",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -98,14 +79,7 @@ export const getRelativeTimeString = (time: number, locale = i18n.language, form
|
|||||||
const dayMillis = hourMillis * 24;
|
const dayMillis = hourMillis * 24;
|
||||||
// Show full date if more than 1 day ago.
|
// Show full date if more than 1 day ago.
|
||||||
if (pastTimeMillis >= dayMillis) {
|
if (pastTimeMillis >= dayMillis) {
|
||||||
return new Date(time).toLocaleDateString(locale, {
|
return getDateTimeString(time, locale);
|
||||||
year: "numeric",
|
|
||||||
month: "numeric",
|
|
||||||
day: "numeric",
|
|
||||||
hour: "numeric",
|
|
||||||
minute: "numeric",
|
|
||||||
second: "numeric",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// numeric: "auto" provides "yesterday" for 1 day ago, "always" provides "1 day ago"
|
// numeric: "auto" provides "yesterday" for 1 day ago, "always" provides "1 day ago"
|
||||||
|
Reference in New Issue
Block a user