0

First I'll say that I've tried the solution in How do I use .toLocaleTimeString() without displaying seconds? and it didn't work for me.

I am trying to create a timer component with the h:m:s format but I always see the full current date. Does anyone have a solution? Here is my code:

import React , { Component } from 'react';

class Timer extends Component {
    constructor (props) {
        super(props);
        this.state = {
            count : 0
        }
    }

    render () {
        {
            var hour = Math.floor(this.state.count/3600),
            minutes = Math.floor((this.state.count - hour*3600)/60),
            seconds = (this.state.count - hour*3600 -minutes*60 ),
            date = new Date();
            date.setHours(hour, minutes, seconds);
        }
        
        return(
            <div>
                {date.toString( {hour: '2-digit', minute:'2-digit'})}
            </div>
        );
    }

}

export default Timer;

What I'm getting from this component looks like this:

enter image description here

Is it related to how react-js is working or am I writing the javascript code wrong?

mashtock
  • 400
  • 4
  • 21

1 Answers1

1

you can do it like this:

//your date
 let temp = new Date(); 
 let HMS = `${temp.getHours()}:${temp.getMinutes()}:${temp.getSeconds()}`

and now you have your hours:minutes:seconds

Dawood Ahmad
  • 476
  • 4
  • 13