Browse Source

Fix for timezone bug in android picker (utc-mode)

master
Henrik Karlsson 6 years ago
parent
commit
23012f8e0d
1 changed files with 18 additions and 5 deletions
  1. +18
    -5
      DatePickerAndroid.js

+ 18
- 5
DatePickerAndroid.js View File

@ -25,8 +25,7 @@ class DatePickerAndroid extends React.Component {
} }
_onChange = e => { _onChange = e => {
const momentDateWithOffset = moment(e.nativeEvent.date).add(this._getOffsetMinutes(), 'minutes')
const jsDate = momentDateWithOffset.toDate()
const jsDate = this._fromIsoWithTimeZoneOffset(e.nativeEvent.date).toDate()
this.props.onDateChange(jsDate) this.props.onDateChange(jsDate)
} }
@ -35,11 +34,25 @@ class DatePickerAndroid extends React.Component {
_minimumDate = () => this._toIsoWithTimeZoneOffset(this.props.minimumDate); _minimumDate = () => this._toIsoWithTimeZoneOffset(this.props.minimumDate);
_date = () => this._toIsoWithTimeZoneOffset(this.props.date); _date = () => this._toIsoWithTimeZoneOffset(this.props.date);
_fromIsoWithTimeZoneOffset = date => {
if (this.props.timeZoneOffsetInMinutes === undefined) {
return moment(date)
}
return moment.utc(date).subtract(this.props.timeZoneOffsetInMinutes, 'minutes')
}
_toIsoWithTimeZoneOffset = date => date && moment(date).add( -this._getOffsetMinutes(), 'minutes').toISOString()
_toIsoWithTimeZoneOffset = date => {
if (!date)
return undefined
if (this.props.timeZoneOffsetInMinutes === undefined) {
return moment(date).toISOString()
}
_getOffsetMinutes = () => this.props.timeZoneOffsetInMinutes === undefined ? 0
: -(this.props.timeZoneOffsetInMinutes + new Date().getTimezoneOffset())
return moment.utc(date).add(this.props.timeZoneOffsetInMinutes, 'minutes')
}
} }

Loading…
Cancel
Save