【问题标题】:Get only seconds from the current date in React.Js component在 React.Js 组件中仅获取当前日期的秒数
【发布时间】:2019-11-21 20:57:53
【问题描述】:

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

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

  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;

【问题讨论】:

标签: javascript reactjs date


【解决方案1】:
constructor(props) {
    super(props);
    this.state = {
      time: new Date().getSeconds()
    };
  }

您可以在日期对象上使用getSeconds(); 方法

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 2018-11-20
    • 1970-01-01
    • 2011-09-03
    • 2023-02-08
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多