Skip to content

Commit 8005d65

Browse files
committed
store and recreate moment with the appropriate timezone
1 parent e8478c7 commit 8005d65

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

source/views/components/datepicker/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Props = {
3030

3131
type State = {
3232
date: moment,
33+
timezone: string,
3334
}
3435

3536
export class DatePicker extends React.Component<any, Props, State> {
@@ -43,15 +44,22 @@ export class DatePicker extends React.Component<any, Props, State> {
4344

4445
state = {
4546
date: this.props.initialDate,
47+
timezone: this.props.initialDate.tz(),
4648
}
4749

4850
componentWillMount() {
49-
this.setState(() => ({date: this.props.initialDate}))
51+
this.setState(() => ({
52+
date: this.props.initialDate,
53+
timezone: this.props.initialDate.tz(),
54+
}))
5055
}
5156

5257
componentWillReceiveProps(nextProps: Props) {
5358
if (nextProps.initialDate !== this.props.initialDate) {
54-
this.setState(() => ({date: nextProps.initialDate}))
59+
this.setState(() => ({
60+
date: nextProps.initialDate,
61+
timezone: nextProps.initialDate.tz(),
62+
}))
5563
}
5664
}
5765

@@ -90,6 +98,7 @@ export class DatePicker extends React.Component<any, Props, State> {
9098
duration={this.props.duration}
9199
height={this.props.height}
92100
minuteInterval={this.props.minuteInterval}
101+
timezone={this.state.timezone}
93102
{...propsToPass}
94103
/>
95104
)

source/views/components/datepicker/ios.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Props = {
2525
mode: 'date' | 'datetime' | 'time',
2626
onDateChange: moment => any,
2727
style?: StyleSheetRules,
28+
timezone: string,
2829
}
2930

3031
type State = {
@@ -68,7 +69,7 @@ export class IosDatePicker extends React.Component<any, Props, State> {
6869
onDateChange = (date: Date) => {
6970
this.setState(() => ({allowPointerEvents: false}))
7071

71-
this.props.onDateChange(moment(date))
72+
this.props.onDateChange(moment.tz(date, this.props.timezone))
7273

7374
const timeoutId = setTimeout(() => {
7475
this.setState(() => ({allowPointerEvents: true}))
@@ -104,6 +105,7 @@ export class IosDatePicker extends React.Component<any, Props, State> {
104105
onDateChange={this.onDateChange}
105106
onHide={this.hideModal}
106107
visible={this.state.modalVisible}
108+
timezone={this.props.timezone}
107109
/>
108110
</View>
109111
</TouchableHighlight>
@@ -120,6 +122,7 @@ type ModalProps = {
120122
visible: boolean,
121123
onDateChange: moment => any,
122124
onHide: () => any,
125+
timezone: string,
123126
}
124127

125128
class DatePickerModal extends React.PureComponent<void, ModalProps, void> {
@@ -141,15 +144,15 @@ class DatePickerModal extends React.PureComponent<void, ModalProps, void> {
141144
onDateChange,
142145
onHide,
143146
visible,
147+
timezone,
144148
} = this.props
145149

146150
let tzOffset = 0
147151
if (date.tz()) {
148152
// We need to negate the offset, because moment inverts the offset for
149153
// POSIX compatability. So, GMT-5 (CST) is shown to be GMT+5.
150154
const dateInUnixMs = date.valueOf()
151-
const tzName = date.tz()
152-
tzOffset = -moment.tz.zone(tzName).offset(dateInUnixMs)
155+
tzOffset = -moment.tz.zone(timezone).offset(dateInUnixMs)
153156
}
154157

155158
return (

0 commit comments

Comments
 (0)