Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React.js中打印当前时间?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何在React.js中打印当前时间?

Javascript 如何在React.js中打印当前时间?,javascript,reactjs,Javascript,Reactjs,在React项目中,我想打印当前运行时间。如何从时间函数中获取数据 const startTime = () => { const now = moment(); const timezone = now.tz(props.timezone) const time = timezone.format('h:mm:ss a'); console.log('CURRENT TIME IS', time) cons

在React项目中,我想打印当前运行时间。如何从时间函数中获取数据

const startTime = () => {
        const now = moment();
        const timezone = now.tz(props.timezone)
        const time = timezone.format('h:mm:ss a');
        console.log('CURRENT TIME IS', time)

        const t = setTimeout(function() {
         return startTime()
        }, 1000);
        return t;
      }

     // startTime() shows 32 or 31 or some number
     //I want to print current time here
     console.log('TIME', startTime())

我正在控制台中获取当前时间。如何在类中的startTime()方法之外打印它。在这种情况下,在
render
方法调用
startTime()
中,只要您想打印当前时间,就可以在其中执行

render() {
   return <div>
            {this.startTime}
          </div>
}
render(){
回来
{this.startTime}
}

从“React”导入React

export class Date extends React.Component {
    constructor() {
        super();
var today = new Date(),
date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
    this.state = {
            date: date
        };
    }

    render() {
        return (
            <div>
                {this.state.date}
            </div>
        );
    }
}
导出类日期扩展React.Component{
构造函数(){
超级();
var today=新日期(),
date=today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
此.state={
日期:日期
};
}
render(){
返回(
{this.state.date}
);
}
}

他要的是时间,不是数据。正当