Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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_Date - Fatal编程技术网

Javascript 在React.Js组件中仅获取距离当前日期的秒数

Javascript 在React.Js组件中仅获取距离当前日期的秒数,javascript,reactjs,date,Javascript,Reactjs,Date,嘿,我只想从当前日期中提取秒数,我有点被阻止了 这是我的组件,它只返回整个日期: class Clock extends Component { constructor(props) { super(props); this.state = { time: new Date().toLocaleString() }; } componentDidMount() { this.i

嘿,我只想从当前日期中提取秒数,我有点被阻止了

这是我的组件,它只返回整个日期:

  class Clock extends Component {
    constructor(props) {
        super(props);
        this.state = {
          time: new Date().toLocaleString()
        };
      }
      componentDidMount() {
        this.intervalID = setInterval(
          () => this.tick(),
          1000
        );
      }
      componentWillUnmount() {
        clearInterval(this.intervalID);
      }
      tick() {
        this.setState({
          time: new Date().toLocaleString()
        });
      }
      render() {
        return (
         <div>{this.state.time}.</div>


        );
      }
     }
  export default Clock;
类时钟扩展组件{
建造师(道具){
超级(道具);
此.state={
时间:新日期().toLocaleString()
};
}
componentDidMount(){
this.intervalID=setInterval(
()=>这个。勾选(),
1000
);
}
组件将卸载(){
clearInterval(此为有效期);
}
勾选(){
这是我的国家({
时间:新日期().toLocaleString()
});
}
render(){
返回(
{this.state.time}。
);
}
}
输出默认时钟;

您可以使用
getSeconds()方法

我不想转换为秒,我只想提取当前日期的当前秒数!您可以使用getSeconds();日期对象上的方法
constructor(props) {
    super(props);
    this.state = {
      time: new Date().getSeconds()
    };
  }