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

2.0 DatePicker 在 this.props.form.getFieldsValue()下返回moment对象时区问题 #3082

Closed
whx76 opened this issue Sep 20, 2016 · 2 comments

Comments

@whx76
Copy link

whx76 commented Sep 20, 2016

发生问题的环境是:

  • antd 版本:2.0.0-beta.6
  • 操作系统及其版本:WIN10
  • 浏览器及其版本:chrome 52

您做了什么?

通过 this.props.form.getFieldsValue()取form中的值,直接序列化为json,提交后台保存。

代码如下:

import 'antd/dist/antd.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { Form, DatePicker, TimePicker, Switch, Radio, Cascader, Slider, Button, Col  } from 'antd';
import moment from 'moment';
const FormItem = Form.Item;
const mountNode = document.getElementById('content');
const CheckboxGroup = Checkbox.Group;


class Demo extends React.Component {
  constructor(props) {
    super(props);
  }
  handleSubmit = (e) => {
    const values = this.props.form.getFieldsValue();
    console.log('formValue', values);
    console.log(JSON.stringify(values));
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <div>
        <Form>
          <FormItem key='FormItem1'
            label="日期"
            >
            {getFieldDecorator('startDate')(
              <DatePicker />
            ) }
          </FormItem>
          <Button type="primary" onClick={this.handleSubmit}>OK</Button>
        </Form>
      </div>
    )
  }
}
Demo = Form.create()(Demo);
ReactDOM.render(<Demo />, mountNode);

您期待的结果是:

DatePicker 中的值和序列化后的时间一样。

实际上的结果是:

现在DatePicker中取值是monment对象返回,序列化为json后少了8个小时,是时区的问题吗?
image

建议this.props.form.getFieldsValue()直接DatePicker返回字符串,对象返回后操作起来复杂化了,易用性下降。

可重现的在线演示

@benjycui
Copy link
Contributor

其实返回 moment 对象好点,比如说一个支持多语言版本的网站,显示给用户看的格式是根据 locale 和时区确定的,发给服务器前再统一为一个格式,可以保证数据库里面的数据格式的一致。

可以使用 JSON.stringify 的第二个参数,当对象为 moment 类型时,调用 .format

@lock
Copy link

lock bot commented May 4, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants