Kim Ber
Kim Ber

Reputation: 89

Can not get hours from the date

I want to get hours, minutes and seconds from date but it throws error.

class Date extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            hours: '',
            minutes: '',
            seconds: ''
        }
        this.getNewDate = this.getNewDate.bind(this);
    }
    getNewDate(){
        let time = new Date();
        let hours = time.getHours();
        let minutes = time.getMinutes();
        let seconds = time.getSeconds();
        this.setState({
            hours: hours,
            minutes: minutes,
            seconds: seconds
        })
    }
    interval = setInterval(this.getNewDate, 1000);
TypeError: time.getHours is not a function

Upvotes: 0

Views: 338

Answers (1)

keul
keul

Reputation: 7819

As you called your component Date, you shadowed the native Date function.

Change the component name.

Upvotes: 4

Related Questions