1

I'm using react-datetime to render a calendar to user for date/time selection. How can I get the parse the value stored in state and turn it into a readable date so I can render that elsewhere in my UI.

I sent date in a post request and the value looks like: 2018-10-26T18:15:47.608Z. When I try to render that time in a table it gets stripped to this value 1540577747608. I want to display the zulu time for now.

      this.state = {
         date: new Date()
      }

      dateChange = date => this.setState({ date });

      render() {
      return (
            <div>
              <label>Choose a start date/time:</label>
              <Datetime
                onChange={this.dateChange}
                value={this.state.date}
                input={false}
                isValidDate={validDate}
                open={true}
                utc={false}
                onClickDay={value => alert("day" + value + "clicked")}
              />
            </div>
          </div>

In a different component where I want to render the date, the value is being retrieved from an api call and being stored formatDate: response.data.data.date

Then, I'm rendering in a table: <td key={schedule.formatDate}>{schedule.formatDate}</td>

1 Answer 1

1

Date does not have easy method for time formatting.
If you want to display the UTC time - or zulu time - you must use the dedicated methods to access hours, minutes and seconds and format them manually.
For example, the following displayes the UTC hours and minutes:

`${event.getUTCHours()}h${event.getUTCMinutes()}`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.