0

Hey i want to extract only the seconds from the current date and i am kind of blocked

This is my component and it only returns the whole date:

  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;
Nathaniel Flick
  • 2,902
  • 2
  • 22
  • 31

1 Answers1

1
constructor(props) {
    super(props);
    this.state = {
      time: new Date().getSeconds()
    };
  }

You can use the getSeconds(); method on the date object

Kevin.a
  • 4,094
  • 8
  • 46
  • 82