Renjith Stephen
Renjith Stephen

Reputation: 95

How can I extract time from Mon Oct 01 2018 02:00:00 GMT+0530 (India Standard Time) in reactjs

Defining State values

constructor(props) {
    super(props);
    this.state = {
      startDate: moment(),
      startTime: moment(),
      endDate: moment(),
      endTime: moment(),
     }
   }

These are my date and time selection components

<FormGroup row>
   <Col md="3"><span>Start Date</span><Datetime dateFormat={true} timeFormat={false} onChange={this.handleStartDate.bind(this)}/></Col>
   <Col md="3"><span>Start Time</span><Datetime dateFormat={false} timeFormat={true} onChange={this.handleStartTime.bind(this)}/></Col>
   <Col md="3"><span>End Date</span><Datetime dateFormat={true} timeFormat={false} onChange={this.handleEndDate.bind(this)}/></Col>
   <Col md="3"><span>End Time</span><Datetime dateFormat={false} timeFormat={true} onChange={this.handleEndTime.bind(this)}/></Col>
</FormGroup>

And the onChange functions are

// start date 
  handleStartDate(date) {
    this.setState({startDate: date});
    this.forceUpdate()
  }
// start time 
  handleStartTime(time) {
    this.setState({ startTime: time });
    this.forceUpdate()
  }
// end date 
  handleEndDate(date) {
    this.setState({ endDate: date });
    this.forceUpdate()
  }
// end time 
  handleEndTime(time) {
    this.setState({ endTime: time });
    this.forceUpdate()
  }

On the final submit button I want to get the time only when selecting the time option, When I'm trying to console the time the returning value is

  submitClick(){
  console.log(this.state.endTime)//returns the value like Mon Oct 01 2018 02:00:00 GMT+0530 (India Standard Time)
}

Please help me to extract time only from Mon Oct 01 2018 02:00:00 GMT+0530 (India Standard Time)

Upvotes: 0

Views: 60

Answers (2)

moment(this.state.endTime).format("h:mm:ss a")

Upvotes: 0

Ivo
Ivo

Reputation: 2510

As your as using the moment library, you can use the format function to suit to your needs.

For example:

console.log(moment(this.state.endTime).format("HH:mm"))

Upvotes: 2

Related Questions