Skip to content

Commit 1980bba

Browse files
mohas22samhere06
authored andcommitted
updated dateTime component to take timezone in consideration
1 parent b3f42c8 commit 1980bba

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

packages/react-sdk-components/src/components/field/DateTime/DateTime.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useState } from 'react';
22
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
33
import dayjs, { Dayjs } from 'dayjs';
4-
4+
import DateFormatter from '../../helpers/formatters/Date';
55
import handleEvent from '../../helpers/event-utils';
66
import { format } from '../../helpers/formatters';
77
import { dateFormatInfoDefault, getDateFormatInfo } from '../../helpers/date-format-utils';
@@ -19,7 +19,10 @@ export default function DateTime(props: DateTimeProps) {
1919

2020
const { getPConnect, label, required, disabled, value = '', validatemessage, status, readOnly, testId, helperText, displayMode, hideLabel } = props;
2121

22-
const [dateValue, setDateValue] = useState<Dayjs | null>(value ? dayjs(value) : null);
22+
const environmentInfo = PCore.getEnvironmentInfo();
23+
const timezone = environmentInfo && environmentInfo.getTimeZone();
24+
25+
const [dateValue, setDateValue] = useState<Dayjs | null>(value ? dayjs(DateFormatter.convertToTimezone(value, { timezone })) : null);
2326

2427
const pConn = getPConnect();
2528
const actions = pConn.getActionsApi();
@@ -35,7 +38,7 @@ export default function DateTime(props: DateTimeProps) {
3538
dateFormatInfo.dateFormatMask = theDateFormat.dateFormatMask;
3639

3740
useEffect(() => {
38-
setDateValue(dayjs(value));
41+
setDateValue(dayjs(DateFormatter.convertToTimezone(value, { timezone })));
3942
}, [value]);
4043

4144
if (displayMode === 'DISPLAY_ONLY') {
@@ -64,8 +67,9 @@ export default function DateTime(props: DateTimeProps) {
6467
};
6568

6669
const handleChange = date => {
67-
setDateValue(date);
68-
const changeValue = date && date.isValid() ? date.toISOString() : null;
70+
const timeZoneDateTime = dayjs.tz(date.format('YYYY-MM-DDTHH:mm:ss'), timezone);
71+
const changeValue = timeZoneDateTime && timeZoneDateTime.isValid() ? timeZoneDateTime.toISOString() : '';
72+
setDateValue(timeZoneDateTime);
6973
handleEvent(actions, 'changeNblur', propName, changeValue);
7074
};
7175

packages/react-sdk-components/src/components/helpers/formatters/common.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export function getLocale(locale: string = '') {
77
return Intl.DateTimeFormat().resolvedOptions().locale;
88
}
99

10-
export function getCurrentTimezone(timezone: string = 'America/New_York') {
11-
if (timezone) return timezone;
10+
export function getCurrentTimezone() {
1211
return PCore?.getLocaleUtils?.().getTimeZoneInUse?.();
1312
}

0 commit comments

Comments
 (0)