# msToTime function

**URL:** <https://discourse.nodered.org/t/mstotime-function/58168>\
**Category:** Share Your Nodes\
**Tags:** function-node\
**Created:** [11 February 2022 17:54 UTC](https://discourse.nodered.org/t/mstotime-function/58168 "2022-02-11T17:54:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [11 February 2022 17:54 UTC](https://discourse.nodered.org/t/mstotime-function/58168/1 "2022-02-11T17:54:12Z")

</div>

I have only 2 or 3 functions defined in my settings.js file (so I can use them anywhere) and I sure do seem to use them. Since time math is always working with milliseconds (wait, duration, periods) I found that constantly converting them to `hh:mm:ss` for the Dashboard or log file became a pain.

I saw on a forum somewhere a small snippet of code called `msToTime` (author was not identified). That triggered the idea for me to create such a function in my `settings.js`.file, (under `functionGlobalContext:` ) so I thought I'd share it.

Once installed there, to call it and use it is as follows:

> USE: context.global.msToTime( number, [boolean1], [boolean2] )
> 
> ```
> number = time (ms) 
> booloean1 (optional) = true || false || null (show ms || no ms || no ms)
> NOTE: if no ms, seconds are rounded down ( 00:00:15:852 = 00:00:15 ) 
> booloean2 (optional) = true || false (JSON out || string out)
> 
> ```
> 
> EXAMPLES:  
> context.global.msToTime(1644591214165) = "52 years, 54 days, 14:53:34"  
> context.global.msToTime(354165) = "00:05:54"  
> context.global.msToTime(354165, true) = "00:05:54:165"  
> context.global.msToTime(1644591214165, true) = "52 years, 54 days, 14:53:34:165"  
> context.global.msToTime(1644591214165, null, true) =  
> {"years":52,"days":54,"hours":14,"minutes":53,"seconds":34,"milliseconds":165}

If you need just hours and minutes only (assuming input # has no years or days) use `substr(0, 5)`:

```auto
var time = {payload: context.global.msToTime(12345678) };
   // result: "03:25:45"
var time = {payload: context.global.msToTime(12345678).substr(0,5) };
   // result: "03:25"

```

Here is the function:

```auto
// , <--- settings.js: comma is needed if another custom function before this.

msToTime: function msToTime(duration, ms, json) 
{
    if (typeof (duration) != 'number') return "NaN";
    if (duration < 0) return "Err < 0";
    var milliseconds = Math.round(duration % 1000),
        seconds = Math.floor((duration / 1000) % 60),
        minutes = Math.floor((duration / (1000 * 60)) % 60),
        hours = Math.floor((duration / (1000 * 60 * 60)) % 24),
        days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 365),
        years = Math.floor((duration / (1000 * 60 * 60 * 24 * 365)) % 365),
        result, json_result;

    years = (years > 0) ? years : 0;
    days = (days > 0) ? days : 0;
    hours = (hours < 10) ? ("00" + hours).slice(-2) : hours;
    minutes = (minutes < 10) ? ("00" + minutes).slice(-2) : minutes;
    seconds = (seconds < 10) ? ("00" + seconds).slice(-2) : seconds;
    milliseconds = ("00" + milliseconds).slice(-3);
    json_result = [{"years": years, "days": days, "hours": Number(hours), "minutes": Number(minutes), "seconds": Number(seconds), "milliseconds": Number(milliseconds) }]; 
    years = (years > 0) ? years + " years, " : "";
    days = (days > 0) ? days + " days, " : "";
    if (ms) result = years + days + hours + ":" + minutes + ":" + seconds + ":" + milliseconds
      else result = years + days + hours + ":" + minutes + ":" + seconds;
    if (json) return json_result 
    else return result;
}

// , <--- settings.js: comma is needed if another custom function after this.

```
