Skip to content

Commit dde0231

Browse files
committed
#933 - Timezone integration in DateField
1 parent b0dcbfd commit dde0231

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
### 🐞 Fixes
1414

15+
- [#933](https://github.com/estruyf/vscode-front-matter/issues/933): Timezone setting integration in the DateTime field
16+
1517
## [10.8.0] - 2025-02-27 - [Release notes](https://beta.frontmatter.codes/updates/v10.8.0)
1618

1719
### 🎨 Enhancements

Diff for: src/helpers/DateHelper.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parse, parseISO, parseJSON, format } from 'date-fns';
2+
import { formatInTimeZone } from 'date-fns-tz';
23

34
export class DateHelper {
45
public static formatUpdate(value: string | null | undefined): string | null {
@@ -19,6 +20,20 @@ export class DateHelper {
1920
return format(date, DateHelper.formatUpdate(dateFormat) as string);
2021
}
2122

23+
public static formatInTimezone(
24+
date?: Date,
25+
dateFormat?: string,
26+
timezone?: string
27+
): string | null {
28+
if (!date || !dateFormat) {
29+
return null;
30+
}
31+
32+
return timezone
33+
? formatInTimeZone(date, timezone, DateHelper.formatUpdate(dateFormat) as string)
34+
: format(date, DateHelper.formatUpdate(dateFormat) as string);
35+
}
36+
2237
public static tryParse(date: any, format?: string): Date | null {
2338
if (!date) {
2439
return null;

Diff for: src/helpers/PanelSettings.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
SETTING_GLOBAL_TIMEZONE,
23
SETTING_PANEL_ACTIONS_DISABLED,
34
SETTING_SPONSORS_AI_ENABLED,
45
SETTING_WEBSITE_URL
@@ -68,7 +69,8 @@ export class PanelSettings {
6869
updateFileName: !!Settings.get<boolean>(SETTING_SLUG_UPDATE_FILE_NAME)
6970
},
7071
date: {
71-
format: Settings.get<string>(SETTING_DATE_FORMAT) || ''
72+
format: Settings.get<string>(SETTING_DATE_FORMAT) || '',
73+
timezone: Settings.get<string>(SETTING_GLOBAL_TIMEZONE) || ''
7274
},
7375
tags: (await TaxonomyHelper.get(TaxonomyType.Tag)) || [],
7476
categories: (await TaxonomyHelper.get(TaxonomyType.Category)) || [],

Diff for: src/models/PanelSettings.ts

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export interface WhenClause {
177177

178178
export interface DateInfo {
179179
format: string;
180+
timezone?: string;
180181
}
181182

182183
export interface SEO {

Diff for: src/panelWebView/components/Fields/DateTimeField.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LocalizationKey } from '../../../localization';
1111

1212
export interface IDateTimeFieldProps extends BaseFieldProps<Date | null> {
1313
format?: string;
14+
timezone?: string;
1415
onChange: (date: string) => void;
1516
}
1617

@@ -30,6 +31,7 @@ export const DateTimeField: React.FunctionComponent<IDateTimeFieldProps> = ({
3031
value,
3132
required,
3233
format,
34+
timezone,
3335
onChange
3436
}: React.PropsWithChildren<IDateTimeFieldProps>) => {
3537
const DEFAULT_FORMAT = 'MM/dd/yyyy HH:mm';
@@ -38,7 +40,7 @@ export const DateTimeField: React.FunctionComponent<IDateTimeFieldProps> = ({
3840
const onDateChange = React.useCallback((date: Date) => {
3941
setDateValue(date);
4042
if (format) {
41-
onChange(DateHelper.format(date, format) || "");
43+
onChange(DateHelper.formatInTimezone(date, format, timezone) || "");
4244
} else {
4345
onChange(date.toISOString());
4446
}

Diff for: src/panelWebView/components/Fields/WrapperField.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
193193
required={!!field.required}
194194
format={field.dateFormat || settings?.date?.format}
195195
onChange={onFieldChange}
196+
timezone={settings?.date?.timezone}
196197
/>
197198
</FieldBoundary>
198199
);

Diff for: src/utils/formatInTimezone.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { format } from 'date-fns';
2-
import { formatInTimeZone } from 'date-fns-tz';
31
import { SETTING_GLOBAL_TIMEZONE } from '../constants';
42
import { DateHelper, Settings } from '../helpers';
53

64
export const formatInTimezone = (date: Date, dateFormat: string) => {
75
const timezone = Settings.get<string>(SETTING_GLOBAL_TIMEZONE);
8-
return timezone
9-
? formatInTimeZone(date, timezone, DateHelper.formatUpdate(dateFormat) as string)
10-
: format(date, DateHelper.formatUpdate(dateFormat) as string);
6+
return DateHelper.formatInTimezone(date, dateFormat, timezone) || '';
117
};

0 commit comments

Comments
 (0)