Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date-picker): ignore timezone in date-picker #2870

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { generateUid } from 'd2/lib/uid';

import getPeriod from 'd2/lib/period/parser';
import PeriodPicker from 'd2-ui/lib/period-picker/PeriodPicker.component';

import { getISOFormatLocalTimestamp } from '../../../utils/date';

const styles = {
periodRow: { display: 'flex' },
Expand All @@ -26,11 +26,6 @@ const styles = {
openDialogButton: { margin: '16px 0' },
};

const getISOFormatLocalTimestamp = value =>
new Date(value.getTime() - value.getTimezoneOffset() * 60000)
.toISOString()
.substring(0, 23);

class DataInputPeriods extends React.Component {
constructor(props, context) {
super(props, context);
Expand Down
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: {
Expand All @@ -27,6 +28,7 @@ export default React.createClass({
...other
} = this.props;


return (
<div style={{ ...this.props.style }}>
<DatePicker
Expand Down Expand Up @@ -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
},
});
},
Expand Down
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-formatted string (without z) 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 ISO-formatted string without timezone information
* @returns date formatted as ISO string without timezone information
*/
export const getISOFormatLocalTimestamp = date =>
new Date(date.getTime() - date.getTimezoneOffset() * 60000)
.toISOString().substring(0,23)
Loading