Skip to content

Commit

Permalink
fix: ensure dates are parsed timezone agnostically
Browse files Browse the repository at this point in the history
Closes #1223
  • Loading branch information
vladislav-karamfilov authored and Skaiir committed Aug 14, 2024
1 parent bea07f4 commit 15b5ba9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
14 changes: 8 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/form-js-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"flatpickr": "^4.6.13",
"ids": "^1.0.5",
"lodash": "^4.17.21",
"luxon": "^3.5.0",
"marked": "^13.0.0",
"min-dash": "^4.2.1",
"preact": "^10.5.14"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useContext, useMemo, useState, useEffect, useRef } from 'preact/hooks';
import { DateTime as LuxonDateTime } from 'luxon';

import classNames from 'classnames';

Expand Down Expand Up @@ -86,7 +87,7 @@ export function Datetime(props) {

switch (subtype) {
case DATETIME_SUBTYPES.DATE: {
date = new Date(Date.parse(value));
date = typeof value === 'string' ? LuxonDateTime.fromISO(value).toJSDate() : new Date(NaN);
break;
}
case DATETIME_SUBTYPES.TIME: {
Expand Down
18 changes: 8 additions & 10 deletions packages/form-js-viewer/src/render/components/util/dateTimeUtil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isNumber } from 'min-dash';
import { MINUTES_IN_DAY, TIME_SERIALISING_FORMATS } from '../../../util/constants/DatetimeConstants';
import { DateTime as LuxonDateTime } from 'luxon';

export const ENTER_KEYDOWN_EVENT = new KeyboardEvent('keydown', {
code: 'Enter',
Expand Down Expand Up @@ -145,18 +146,15 @@ export function parseIsoTime(isoTimeString) {
}
}

/**
* Returns the date object as a simple 'YYYY-MM-DD' formatted date in the local timezone.
*
* @param {*} date The date object to serialize.
* @returns {string} The serialized date.
*/
export function serializeDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;

return [year, month, day].join('-');
return LuxonDateTime.fromJSDate(date).toISODate();
}

// this method is used to make the `new Date(value)` parsing behavior stricter
export function isDateTimeInputInformationSufficient(value) {
if (!value || typeof value !== 'string') return false;
Expand Down

0 comments on commit 15b5ba9

Please sign in to comment.