• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

241
Views
Javascript get Date format in H:M:S using react js

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?

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error