Skip to content

Commit

Permalink
fix(date-picker): ignore timezone in date-picker
Browse files Browse the repository at this point in the history
Birkbjo committed Jun 3, 2024
1 parent ba3dae7 commit 314e102
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/forms/form-fields/date-select.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import DatePicker from 'material-ui/DatePicker/DatePicker';
import IconButton from 'material-ui/IconButton/IconButton';
import { getISOFormatLocalTimestamp } from '../../utils/date';

export default React.createClass({
propTypes: {
@@ -27,6 +28,7 @@ export default React.createClass({
...other
} = this.props;


return (
<div style={{ ...this.props.style }}>
<DatePicker
@@ -74,9 +76,11 @@ export default React.createClass({
},

_onDateSelect(event, date) {
// selector gives date in local time, but we want to remove "timezone"-info
const correctedLocalDate = getISOFormatLocalTimestamp(date);
this.props.onChange({
target: {
value: date,
value: correctedLocalDate
},
});
},
11 changes: 11 additions & 0 deletions src/utils/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Formats the date to an ISO string where the timezone of date is ignored before conversion to UTC.
*
* This is useful for date-selectors where we get information with timezone information,
* but we are just interested in the date itself, and don't want the time to be adjusted when converting to UTC.
* @param {Date} date - the date to convert to an UTC ISO string
* @returns date formatted as ISO string without timezone information
*/
export const getISOFormatLocalTimestamp = date =>
new Date(date.getTime() - date.getTimezoneOffset() * 60000)
.toISOString()

0 comments on commit 314e102

Please sign in to comment.