简体   繁体   中英

Convert String to Date in React

I retrieve a date variable from a JSON object in a string format, but I am having trouble getting the elapsed time until now.

import React from 'react';

export default class Date extends React.Component {
render() {

    console.log(this.props.date); //shows: 2017-01-25T10:18:18Z

    var date = Date.parse(this.props.date.toString());

    console.log(date.getTime());

    return (
        <div >
      </div>
    );
   }
}

If its not a date object, just create a new date object.

 var date = new Date(this.props.date); 
 var elapsed = date.geTime(); // Elapsed time in MS

If it is just call this.props.date.getTime()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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