Javascript 在 React 中将字符串转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41854215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:41:56  来源:igfitidea点击:

Convert String to Date in React

javascriptjsonreactjs

提问by Nedjim DN

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

我从字符串格式的 JSON 对象中检索日期变量,但直到现在我都无法获取经过的时间。

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>
    );
   }
}

回答by Chase

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()

如果只是调用 this.props.date.getTime()