Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to calculate the difference between two time periods in TypeScript

1 Answer

0 votes
class TimeDiff
{
    seconds = 0;
    minutes = 0;
    hours = 0;
    constructor(hours : number, minutes : number, seconds : number) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }
    static timeDifference(start : TimeDiff, end : TimeDiff) {
        let timediff = new TimeDiff(0, 0, 0);
        if (start.seconds > end.seconds) {
            end.minutes--;
            end.seconds += 60;
        }
        timediff.seconds = end.seconds - start.seconds;
        if (start.minutes > end.minutes) {
            end.hours--;
            end.minutes += 60;
        }
        timediff.minutes = end.minutes - start.minutes;
        timediff.hours = end.hours - start.hours;
         
        return timediff;
    }
}

let start = new TimeDiff(7, 11, 25);
let end = new TimeDiff(11, 32, 17);
let timediff = TimeDiff.timeDifference(start, end);
 
console.log(timediff.hours + ":" + timediff.minutes + ":" + timediff.seconds);

 
 
 
 
/*
run:
 
"4:20:52"
 
*/

 



answered Sep 16, 2022 by avibootz
edited Sep 16, 2022 by avibootz

Related questions

1 answer 126 views
1 answer 120 views
1 answer 130 views
1 answer 134 views
1 answer 132 views
2 answers 208 views
...