Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Clock Timer is showing wrong time JavaScript

I am working on timer when user clock in the timer starts. It is working fine until it reaches the 60 minutes figure and it keeps on going doesn’t added hour. Here is my code

  export enum HMS{
  hours=3600,
  MinSec=60,
}

 transform(value: number = 3600): any {
// here values is in minutes
    const hours: number = Math.floor(value / HMS.hours)
    const minutes: number = Math.floor(value / HMS.MinSec);
    const seconds: number = (value - minutes * HMS.MinSec);
    return ((hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds)
  }

Below is the image display time. It shows like this.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You definitely need a Modulo in there:

export enum HMS{
      hours=3600,
      MinSec=60,
    }
    
transform(value: number = 3600): any {
    // here values is in minutes
    const hours: number = Math.floor(value / HMS.hours)
    const minutes: number = Math.floor(value / HMS.MinSec % 60);
    const seconds: number = Math.floor(value % 60);
    return ((hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading