繁体   English   中英

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

[英]Get only seconds from the current date in React.Js component

嘿,我只想从当前日期中提取秒数,但我有点被屏蔽了

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

  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;
constructor(props) {
    super(props);
    this.state = {
      time: new Date().getSeconds()
    };
  }

您可以使用getSeconds(); 日期 object 上的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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