简体   繁体   English

如何将 UTC 日期转换为 react-datepicker 的“MM/DD/YYYY”?

[英]How to convert a UTC date to “MM/DD/YYYY” for react-datepicker?

I use react-datepicker in a component for updating posts.我在组件中使用 react-datepicker 来更新帖子。 The date and other passed to the component from MongoDB through props.This is a simple fullstack-graphql application with a CRUD functionality.日期和其他从 MongoDB 通过 props 传递给组件。这是一个简单的 fullstack-graphql 应用程序,具有 CRUD 功能。 Maybe the problem is with correct date conversion.也许问题在于正确的日期转换。 The format of the input date received through props is Unix Timestamp, like "1573227729064".通过 props 接收到的输入日期格式为 Unix Timestamp,如“1573227729064”。 The "datepicker" format should be "MM/DD/YYYY". “日期选择器”格式应为“MM/DD/YYYY”。 I have read the docs and configure datepicker, but for some reason it doesn't render.我已经阅读了文档并配置了 datepicker,但由于某种原因它没有呈现。 Maybe someone can explain me, looking at the code below.也许有人可以解释我,看看下面的代码。 Will be very grateful for any help!将非常感谢任何帮助!

import React, { Component } from 'react';
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import { EditorState, getCurrentContent, getPlainText, ContentState } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
import '../../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import { stateToHTML } from 'draft-js-export-html';
import gql from 'graphql-tag';
import { Mutation } from 'react-apollo';
import { stateFromHTML } from 'draft-js-import-html';

const updateMutation = gql`
  mutation UpdateHeroMutation($id: ID!, $title: String!, $description: String!, $date: String!) {
  updateHero(heroUpdate: {_id: $id, title: $title, description: $description, date: $date}) {
    _id
    title
    description
    date
  }
}
`;

class Hero extends Component {
  constructor(props) {
    super(props);
    this.state = {
      title: props.data.title,
      setDate: props.data.date,
      editorState: EditorState.createWithContent(stateFromHTML(props.data.description))
    };
  }
  onChange(e) {
    this.setState({ title: e.target.value })
  }
  onChangeSelect(published) {
    this.setState({ setDate: published })
  }
  onEditorStateChange = (editorState) => {
    this.setState({
      editorState
    });
  }
  render() {
    return (
      <>
        <h5>Edit post</h5>
        <div className="label-section">Name:</div>
        <h5>
          <input name="title" id="title" type="text" defaultValue={this.props.data.title} onChange={e => this.onChange(e)} />
        </h5>
        <br />
        <div className="label-section">Published:</div>
        <DatePicker
          id="published"
          name="published"
          defaultValue=""
          dateFormat="MM/DD/YYYY"
          selected={Date(this.state.setDate)}
          onChange={published => this.onChangeSelect(published)}
        />
        <h5>{this.state.setDate}</h5>
        <div className="label-section">Edit text:</div>
        <Editor
          editorState={this.state.editorState}
          wrapperClassName="demo-wrapper"
          editorClassName="demo-editor"
          onEditorStateChange={this.onEditorStateChange}
        />
        <input type="text" className="hidden" defaultValue={this.props.data._id} />
        <Mutation mutation={updateMutation} variables={{ id: this.props.data._id, title: this.state.title, description: stateToHTML(this.state.editorState.getCurrentContent()), date: this.state.setDate }}>
          {updateHero => (
            <button onClick={updateHero} type="submit">OK</button>
          )}
        </Mutation>
      </>
    );
  }
}

export default Hero;

You need to initialize the date in您需要在

this.state = {
  title: props.data.title,
  setDate: new Date(props.data.date),
  editorState: EditorState.createWithContent(stateFromHTML(props.data.description))
};

and

<DatePicker
      id="published"
      name="published"
      defaultValue=""
      dateFormat="MM/DD/YYYY"
      selected={this.state.setDate}
      onChange={this.onChangeSelect}
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM