# I make a downtime timer. it operates similarly to stopwatch. but the problem is i cant display the timer in hour:minute:seconds . after 60sec it continues to 61 instead 1:01. can anyone help me?

**URL:** <https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366>\
**Category:** General\
**Created:** [30 August 2021 01:21 UTC](https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366 "2021-08-30T01:21:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xpqrs97](https://avatars.discourse-cdn.com/v4/letter/x/f4b2a3/32.png) [@xpqrs97](https://discourse.nodered.org/u/xpqrs97)\
**Post date:** [30 August 2021 01:21 UTC](https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366/1 "2021-08-30T01:21:24Z")

</div>

![downtime java](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65c72348e4c0e5f8931fb39bb3c55d2be10fe264.png)  
 ![downtime](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/309e1f1614368d0c893724b488a1e574dbb76fd9.png)

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [30 August 2021 02:06 UTC](https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366/2 "2021-08-30T02:06:31Z")

</div>

Hello and welcome to the forum.

Your question is not really Node-Red specific... but more of a _"How program a thing in javascript"_

There are many options out there that can easily be put in a function node.

[https://www.google.com/search?q=javascript+timer+hours+minutes+seconds](https://www.google.com/search?q=javascript+timer+hours+minutes+seconds)

And then there are many timer nodes available. Search for **timer** in the palette for one that might work for your needs.

---

<div class="post-metadata">

**Author:** ![ChZeman](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ChZeman](https://discourse.nodered.org/u/ChZeman)\
**Post date:** [3 September 2021 13:07 UTC](https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366/4 "2021-09-03T13:07:45Z")

</div>

I have a function I found online that I use to calculate downtime duration. I eliminated seconds from it because I didn't need it for my purposes.

In my case, I have a Pi running Node Red at each of our machines that reads PLC data and saves it to a MySQL db at our central status display. When a machine has an error, the Pi creates a timestamp using the function getDateTime() and saves that to the database. The central display sees that timestamp, gets the current date and time and calls the function timeDiffCalc(Now, Last\_Update) to create a string to display the downtime.

I hope this helps.

Chris

```auto
function getDateTime() {
    var now = new Date(); 
    var year = now.getFullYear();
    var month = now.getMonth()+1; 
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds(); 

    if(month.toString().length == 1) {
        month = '0'+month;
    }
    
    if(day.toString().length == 1) {
        day = '0'+day;
    }   

    if(hour.toString().length == 1) {
        hour = '0'+hour;
    }
    
    if(minute.toString().length == 1) {
        minute = '0'+minute;
    }
    
    if(second.toString().length == 1) {
        second = '0'+second;
    }   
    var dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;   
    return dateTime;
}

function timeDiffCalc(Now, Last_Update) {
    let diffInMilliSeconds = Math.abs(Now - Last_Update) / 1000;

    // calculate days
    const days = Math.floor(diffInMilliSeconds / 86400);
    diffInMilliSeconds -= days * 86400;
    console.log('calculated days', days);

    // calculate hours
    const hours = Math.floor(diffInMilliSeconds / 3600) % 24;
    diffInMilliSeconds -= hours * 3600;
    console.log('calculated hours', hours);

    // calculate minutes
    const minutes = Math.floor(diffInMilliSeconds / 60) % 60;
    diffInMilliSeconds -= minutes * 60;
    console.log('minutes', minutes);

    let difference = '';
    if (days > 0) {
      difference += (days === 1) ? `${days} Day, ` : `${days} Days, `;
    }

    if (hours > 0) {
        difference += (hours === 0 || hours === 1) ? `${hours} Hour, ` : `${hours} Hours, `;
    }

    difference += (minutes === 0 || hours === 1) ? `${minutes} Minutes` : `${minutes} Minutes`; 

    return difference;
}

timeDiffCalc(new Date(Now), new Date(Last_Update))

```

---

<div class="post-metadata">

**Author:** ![xpqrs97](https://avatars.discourse-cdn.com/v4/letter/x/f4b2a3/32.png) [@xpqrs97](https://discourse.nodered.org/u/xpqrs97)\
**Post date:** [6 September 2021 00:05 UTC](https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366/5 "2021-09-06T00:05:58Z")

</div>

thank you for the help!. I have already found the way to solve this. i just create a function and call it in the msg.payload

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 September 2021 00:06 UTC](https://discourse.nodered.org/t/i-make-a-downtime-timer-it-operates-similarly-to-stopwatch-but-the-problem-is-i-cant-display-the-timer-in-hourseconds-after-60sec-it-continues-to-61-instead-1-01-can-anyone-help-me/50366/6 "2021-09-20T00:06:33Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
