Skip to content

Commit 52309dd

Browse files
author
SvenPf
committed
fix issue where date formatting was wrong if machine's locale is anything other then 'en' or 'gb'
1 parent 0514ce5 commit 52309dd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/utils/DateFormatter.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const moment = obsidian.moment;
66

77
export class DateFormatter {
88
toFormat: string;
9-
localeString: string;
9+
locale: string;
1010

1111
constructor() {
1212
this.toFormat = 'YYYY-MM-DD';
13-
// get locale string (e.g. en, en-gb, de, fr, etc.)
14-
this.localeString = new Intl.DateTimeFormat().resolvedOptions().locale;
13+
// get locale of this machine (e.g. en, en-gb, de, fr, etc.)
14+
this.locale = new Intl.DateTimeFormat().resolvedOptions().locale;
1515
}
1616

1717
setFormat(format: string): void {
@@ -20,13 +20,12 @@ export class DateFormatter {
2020

2121
getPreview(format?: string): string {
2222
const today = moment();
23-
today.locale(this.localeString);
2423

2524
if (!format) {
2625
format = this.toFormat;
2726
}
2827

29-
return today.format(format);
28+
return today.locale(this.locale).format(format);
3029
}
3130

3231
/**
@@ -36,9 +35,11 @@ export class DateFormatter {
3635
* @param dateString the date string to be formatted
3736
* @param dateFormat the current format of `dateString`. When this is `null` and the actual format of the
3837
* given date string is not `C2822` or `ISO` format, this function will try to guess the format by using the native `Date` module.
38+
* @param locale the locale of `dateString`. This is needed when `dateString` includes a month or day name and its locale format differs
39+
* from the locale of this machine.
3940
* @returns formatted date string or null if `dateString` is not a valid date
4041
*/
41-
format(dateString: string, dateFormat?: string): string | null {
42+
format(dateString: string, dateFormat?: string, locale: string = 'en'): string | null {
4243
if (!dateString) {
4344
return null;
4445
}
@@ -47,20 +48,20 @@ export class DateFormatter {
4748

4849
if (!dateFormat) {
4950
// reading date formats other then C2822 or ISO with moment is deprecated
51+
// see https://momentjs.com/docs/#/parsing/string/
5052
if (this.hasMomentFormat(dateString)) {
5153
// expect C2822 or ISO format
5254
date = moment(dateString);
5355
} else {
5456
// try to read date string with native Date
5557
date = moment(new Date(dateString));
5658
}
57-
date.locale(this.localeString); // set local locale definition for moment
5859
} else {
59-
date = moment(dateString, dateFormat, this.localeString);
60+
date = moment(dateString, dateFormat, locale);
6061
}
6162

6263
// format date (if it is valid)
63-
return date.isValid() ? date.format(this.toFormat) : null;
64+
return date.isValid() ? date.locale(this.locale).format(this.toFormat) : null;
6465
}
6566

6667
private hasMomentFormat(dateString: string): boolean {

0 commit comments

Comments
 (0)