# Light weight library free time-date-string-functions (hour, min, sec, day, month with leading 0)

**URL:** <https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913>\
**Category:** Share Your Nodes\
**Created:** [15 December 2024 14:24 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913 "2024-12-15T14:24:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [15 December 2024 14:24 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/1 "2024-12-15T14:24:09Z")

</div>

![Screenshot 2024-12-15 152102](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13f1664cfdf286bb528813a2c285644e22eb5dc8.png)

```auto
[{"id":"7a8f4d515c569ef4","type":"group","z":"93d94ffa17becd25","name":"light weight library free time-date-string-functions (hour, min, sec, day, month with leading 0)","style":{"fill":"#ffefbf","fill-opacity":"0.18","label":true},"nodes":["9349197e3ec3c38c","a5d5a1231ebcadad","2e5a1059caa7ef3f","d9fb9044a7fc6a11","bfb14c881c262fc7","9ce7f0169a0680a1","dd252713d82813ec","ed3b13bb082cccfa","7699a7d4d69b4430","179de3c0ebc7f694","afc0fb0cc7757c97","cef33a6d671c3fb0","3270715a84e162ca","222f95b48ab65c83","35ef29af4074f9d5","018865504fb5dbd6","913ab0a902d8c5d4","9a5300c29e8af76b"],"x":114,"y":39,"w":652,"h":382},{"id":"9349197e3ec3c38c","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"hh:mm:ss","func":"msg.payload = date_time_string(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction date_time_string(timestamp){\n let text = new Date(timestamp).toLocaleTimeString();\n let text_a = text.split(':');\n const hours = (\"0\" + text_a[0]).slice(-2);\n const minutes = (\"0\" + text_a[1]).slice(-2);\n const seconds = (\"0\" + text_a[2]).slice(-2); \n return ( hours + ':' + minutes + ':' + seconds ); \n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":140,"wires":[["2e5a1059caa7ef3f"]]},{"id":"a5d5a1231ebcadad","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":140,"wires":[["9349197e3ec3c38c"]]},{"id":"2e5a1059caa7ef3f","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2527","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":140,"wires":[]},{"id":"d9fb9044a7fc6a11","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"DD.MM.YYYY","func":"msg.payload = date_string(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\n\nfunction date_string(timestamp){\n let text_a = new Date(msg.payload).toLocaleDateString();\n text_a = text_a.split('.');\n const day = (\"0\" + text_a[0]).slice(-2);\n const month = (\"0\" + text_a[1]).slice(-2);\n const year = text_a[2]; \n return ( day + '.' + month + '.' + year ); \n}\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":200,"wires":[["9ce7f0169a0680a1"]]},{"id":"bfb14c881c262fc7","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":200,"wires":[["d9fb9044a7fc6a11"]]},{"id":"9ce7f0169a0680a1","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2528","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":200,"wires":[]},{"id":"dd252713d82813ec","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"hh:mm:ss DD.MM.YYYY","func":"const x = time_date_string(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: x});\nreturn{payload:x};\n\nfunction time_date_string(time) {\n let x = new Date(time).toLocaleString();\n x = x.split(',');\n\n let d = x[0].trim();\n d = d.split('.');\n const day = (\"0\" + d[0]).slice(-2);\n const month = (\"0\" + d[1]).slice(-2);\n const year = d[2];\n\n let t = x[1].trim();\n t = t.split(':');\n const hours = (\"0\" + t[0]).slice(-2);\n const minutes = (\"0\" + t[1]).slice(-2);\n const seconds = (\"0\" + t[2]).slice(-2); \n\n return (hours + ':' + minutes + ':' + seconds + \" \" + day + '.' + month + '.' + year);\n //return (day + '.' + month + '.' + year + \" \" + hours + ':' + minutes + ':' + seconds); \n} ","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":260,"wires":[["7699a7d4d69b4430"]]},{"id":"ed3b13bb082cccfa","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":260,"wires":[["dd252713d82813ec"]]},{"id":"7699a7d4d69b4430","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2533","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":260,"wires":[]},{"id":"179de3c0ebc7f694","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"hh:mm","func":"msg.payload = date_time_string(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction date_time_string(timestamp){\n let text = new Date(timestamp).toLocaleTimeString();\n let text_a = text.split(':');\n const hours = (\"0\" + text_a[0]).slice(-2);\n const minutes = (\"0\" + text_a[1]).slice(-2);\n return ( hours + ':' + minutes); \n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":80,"wires":[["cef33a6d671c3fb0"]]},{"id":"afc0fb0cc7757c97","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":80,"wires":[["179de3c0ebc7f694"]]},{"id":"cef33a6d671c3fb0","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2537","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":80,"wires":[]},{"id":"3270715a84e162ca","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":320,"wires":[["222f95b48ab65c83"]]},{"id":"222f95b48ab65c83","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"format based time strings","func":"const x = time_date_string(msg.payload, \"HH:MM:SS @ DD-MM-YYYY\");\nnode.status({ fill: \"red\", shape: \"dot\", text: x});\nreturn{payload:x};\n\nfunction time_date_string(time, format) {\n let x = new Date(time).toLocaleString();\n x = x.split(',');\n\n let d = x[0].trim();\n d = d.split('.');\n const day = (\"0\" + d[0]).slice(-2);\n const month = (\"0\" + d[1]).slice(-2);\n const year = d[2];\n\n let t = x[1].trim();\n t = t.split(':');\n const hours = (\"0\" + t[0]).slice(-2);\n const minutes = (\"0\" + t[1]).slice(-2);\n const seconds = (\"0\" + t[2]).slice(-2); \n\n switch (format){ \n case \"HH:MM:SS @ DD-MM-YYYY\":\n return (hours + ':' + minutes + ':' + seconds + \" @ \" + day + \"-\" + month + \"-\" + year);\n case \"HH:MM:SS DD-MM-YYYY\":\n return (hours + ':' + minutes + ':' + seconds + \" \" + day + \"-\" + month + \"-\" + year);\n case \"DD-MM-YYYY\":\n return (day + \"-\" + month + \"-\" + year);\n case \"HH:MM:SS\":\n return (hours + ':' + minutes + ':' + seconds);\n case \"HH:MM\":\n return (hours + ':' + minutes);\n }\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":320,"wires":[["35ef29af4074f9d5"]]},{"id":"35ef29af4074f9d5","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2538","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":320,"wires":[]},{"id":"018865504fb5dbd6","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"runtime input ms","func":"msg.payload = runtime_str(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction runtime_str(ms) {\n const t = ms / 1000;\n const h = (Math.floor(t / 3600)).toString();\n const m = (Math.floor(t % 3600 / 60)).toString();\n const s = (Math.floor(t % 3600 % 60)).toString();\n return (h + \":\" + (\"0\" + m).slice(-2) + \":\" + (\"0\" + s).slice(-2));\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":380,"wires":[["9a5300c29e8af76b"]]},{"id":"913ab0a902d8c5d4","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"86400000","payloadType":"num","x":220,"y":380,"wires":[["018865504fb5dbd6"]]},{"id":"9a5300c29e8af76b","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2539","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 December 2024 16:50 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/2 "2024-12-15T16:50:32Z")

</div>

Hi,

Thanks for the share.

I only took a very quick look at this - I see you use `toLocalexxx` then split the parts out of the date string. Your code relied on the assumption that the date format would always be `DD.MM.YYYY` however, in some locales, the date format might be `MM/DD/YYYY` or `YYYY-MM-DD`.

For example, one of your original functions was like this:

```auto
const x = time_date_string(msg.payload);
node.status({ fill: "red", shape: "dot", text: x});
return{payload:x};

function time_date_string(time) {
    let x = new Date(time).toLocaleString();
    x = x.split(',');

    let d = x[0].trim();
    d = d.split('.');
    const day = ("0" + d[0]).slice(-2);
    const month = ("0" + d[1]).slice(-2);
    const year = d[2];

    let t = x[1].trim();
    t = t.split(':');
    const hours = ("0" + t[0]).slice(-2);
    const minutes = ("0" + t[1]).slice(-2);
    const seconds = ("0" + t[2]).slice(-2);

    return (hours + ':' + minutes + ':' + seconds + " " + day + '.' + month + '.' + year);
}

```

A more globally correct approach would be to use the built in date functions (like `getHour`) to get the parts of the date-time, then re-assemble them to your desired format e.g...

```auto
const x = formatDateTime(msg.payload);
node.status({ fill: "red", shape: "dot", text: x });
return { payload: x };

function formatDateTime(timestamp) {
  const date = new Date(timestamp);

  // Get date and time components with zero-padding
  const day = date.getDate().toString().padStart(2, '0');
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const year = date.getFullYear().toString();
  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');

  // Assemble the formatted string using template literals
  return `${hours}:${minutes}:${seconds} ${day}.${month}.${year}`;
}

```

Of course, you could use the built in `moment` package in a change node, or import `date-fns` in a function node to avoid all of the reinventing of the wheel.

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [15 December 2024 17:09 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/3 "2024-12-15T17:09:08Z")

</div>

Hi Steve,

first, I wanted to avoid moment.  
second, yes, I have these functions working.  
I just played with to localexxx and thought I could use the old approach with day month etc.

```auto
function date_time_string(timestamp){
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const hours = ("0" + date.getHours()).slice(-2);
    const minutes = ("0" + date.getMinutes()).slice(-2);
    const seconds = ("0" + date.getSeconds()).slice(-2);
    return ( day + "-" + month + "-" + year + " " + hours + ':' + minutes + ':' + seconds ); 
}

```

but I like your padstart 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 December 2024 21:46 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/4 "2024-12-15T21:46:19Z")

</div>

> [@Steve-Mcl](#):
>
> Of course, you could use the built in `moment` package in a change node, or import `date-fns` in a function node to avoid all of the reinventing of the wheel.

or `dayjs` which is somewhat compatible with moment.  
a 'library' means it has multiple functions, this 'library' has only one function and it is not flexible. To start, what if you don't want a padded `0` ?

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [16 December 2024 11:59 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/5 "2024-12-16T11:59:51Z")

</div>

based on Steve's input I rewrote the functions.  
easy to copy them into your own function nodes

```auto
[{"id":"7a8f4d515c569ef4","type":"group","z":"93d94ffa17becd25","name":"light weight library free time-date-string-functions (hour, min, sec with leading 0)","style":{"fill":"#ffefbf","fill-opacity":"0.18","label":true},"nodes":["9349197e3ec3c38c","a5d5a1231ebcadad","2e5a1059caa7ef3f","d9fb9044a7fc6a11","bfb14c881c262fc7","9ce7f0169a0680a1","dd252713d82813ec","ed3b13bb082cccfa","7699a7d4d69b4430","179de3c0ebc7f694","afc0fb0cc7757c97","cef33a6d671c3fb0","3270715a84e162ca","222f95b48ab65c83","35ef29af4074f9d5","018865504fb5dbd6","913ab0a902d8c5d4","9a5300c29e8af76b"],"x":114,"y":39,"w":652,"h":382},{"id":"9349197e3ec3c38c","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"hh:mm:ss","func":"msg.payload = formatDateTime(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction formatDateTime(timestamp) {\n const date = new Date(timestamp);\n const hours = date.getHours().toString().padStart(2, '0');\n const minutes = date.getMinutes().toString().padStart(2, '0');\n const seconds = date.getSeconds().toString().padStart(2, '0');\n return `${hours}:${minutes}:${seconds}`;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":140,"wires":[["2e5a1059caa7ef3f"]]},{"id":"a5d5a1231ebcadad","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":140,"wires":[["9349197e3ec3c38c"]]},{"id":"2e5a1059caa7ef3f","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2527","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":140,"wires":[]},{"id":"d9fb9044a7fc6a11","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"DD.MM.YYYY","func":"msg.payload = formatDate(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction formatDate(timestamp) {\n const date = new Date(timestamp);\n const day = date.getDate().toString().padStart(2, '0');\n const month = (date.getMonth() + 1).toString().padStart(2, '0');\n const year = date.getFullYear().toString();\n return `${day}.${month}.${year}`;\n}\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":200,"wires":[["9ce7f0169a0680a1"]]},{"id":"bfb14c881c262fc7","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":200,"wires":[["d9fb9044a7fc6a11"]]},{"id":"9ce7f0169a0680a1","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2528","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":200,"wires":[]},{"id":"dd252713d82813ec","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"hh:mm:ss DD.MM.YYYY","func":"const t_d = time_date_string(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text:t_d});\nreturn{payload:t_d};\n\nfunction time_date_string(time_date) {\n const date = new Date(time_date);\n const day = date.getDate().toString().padStart(2, '0');\n const month = (date.getMonth() + 1).toString().padStart(2, '0');\n const year = date.getFullYear().toString();\n const hours = date.getHours().toString().padStart(2, '0');\n const minutes = date.getMinutes().toString().padStart(2, '0');\n const seconds = date.getSeconds().toString().padStart(2, '0');\n return `${hours}:${minutes}:${seconds} ${day}.${month}.${year}`;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":260,"wires":[["7699a7d4d69b4430"]]},{"id":"ed3b13bb082cccfa","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":260,"wires":[["dd252713d82813ec"]]},{"id":"7699a7d4d69b4430","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2533","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":260,"wires":[]},{"id":"179de3c0ebc7f694","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"hh:mm","func":"msg.payload = formatDateTime(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction formatDateTime(timestamp) {\n const date = new Date(timestamp);\n const hours = date.getHours().toString().padStart(2, '0');\n const minutes = date.getMinutes().toString().padStart(2, '0');\n return `${hours}:${minutes}`;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":80,"wires":[["cef33a6d671c3fb0"]]},{"id":"afc0fb0cc7757c97","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":80,"wires":[["179de3c0ebc7f694"]]},{"id":"cef33a6d671c3fb0","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2537","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":80,"wires":[]},{"id":"3270715a84e162ca","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":320,"wires":[["222f95b48ab65c83"]]},{"id":"222f95b48ab65c83","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"format based time strings","func":"const x = time_date_string(msg.payload, \"HH:MM:SS @ DD-MM-YYYY\");\nnode.status({ fill: \"red\", shape: \"dot\", text: x});\nreturn{payload:x};\n\nfunction time_date_string(time_date,format) {\n const date = new Date(time_date);\n const day = date.getDate().toString().padStart(2, '0');\n const month = (date.getMonth() + 1).toString().padStart(2, '0');\n const year = date.getFullYear().toString();\n const hours = date.getHours().toString().padStart(2, '0');\n const minutes = date.getMinutes().toString().padStart(2, '0');\n const seconds = date.getSeconds().toString().padStart(2, '0');\n\n switch (format){ \n case \"HH:MM:SS @ DD-MM-YYYY\":\n return `${hours}:${minutes}:${seconds}${\" @ \"}${day}.${month}.${year}`;\n case \"HH:MM:SS DD.MM.YYYY\":\n return `${hours}:${minutes}:${seconds} ${day}.${month}.${year}`;\n case \"DD.MM.YYYY\":\n return `${day}.${month}.${year}`;\n case \"HH:MM:SS\":\n return `${hours}:${minutes}:${seconds}`;\n case \"HH:MM\":\n return `${hours}:${minutes}`;\n }\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":320,"wires":[["35ef29af4074f9d5"]]},{"id":"35ef29af4074f9d5","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2538","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":320,"wires":[]},{"id":"018865504fb5dbd6","type":"function","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"runtime input ms","func":"msg.payload = runtime_str(msg.payload);\nnode.status({ fill: \"red\", shape: \"dot\", text: msg.payload });\nreturn msg;\n\nfunction runtime_str(ms) {\n const t = ms / 1000;\n const h = (Math.floor(t / 3600)).toString();\n const m = (Math.floor(t % 3600 / 60)).toString().padStart(2, '0');\n const s = (Math.floor(t % 3600 % 60)).toString().padStart(2, '0');\n return `${h}:${m}:${s}`;\n} ","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":380,"wires":[["9a5300c29e8af76b"]]},{"id":"913ab0a902d8c5d4","type":"inject","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"86300000","payloadType":"num","x":220,"y":380,"wires":[["018865504fb5dbd6"]]},{"id":"9a5300c29e8af76b","type":"debug","z":"93d94ffa17becd25","g":"7a8f4d515c569ef4","name":"debug 2539","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 December 2024 14:19 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/6 "2024-12-16T14:19:13Z")

</div>

Or of course use [node-red-contrib-simpletime](https://flows.nodered.org/node/node-red-contrib-simpletime) ...

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [16 December 2024 15:44 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/7 "2024-12-16T15:44:03Z")

</div>

> [@Steve-Mcl](#):
>
> use the built in `moment` package in a change node, or import `date-fns` in a function node

Is there a reason not to use moment in a function node?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 December 2024 15:53 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/8 "2024-12-16T15:53:15Z")

</div>

> [@mikefila](#):
>
> Is there a reason not to use moment in a function node

I cant remember if moment is actually available in a function node - you might need to import it on the setup tab.

But since it is available in the change node out of the box (using JSONata) I don't bother with function node.

In fact I rarely do any date formatting!

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 December 2024 16:36 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/9 "2024-12-16T16:36:40Z")

</div>

> [@mikefila](#):
>
> Is there a reason not to use moment in a function node?

Moment.js is a legacy project, now in maintenance mode.  
If you check moment's [npm page](https://www.npmjs.com/package/moment) even they recommend;

> In most cases, you should choose a different library.

So maybe not a great choice to use it anywhere in a new project.

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [16 December 2024 16:51 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/10 "2024-12-16T16:51:07Z")

</div>

> [@Paul-Reed](#):
>
> Moment.js is a legacy project, now in maintenance mode.  
> ...  
> So maybe not a great choice to use it anywhere.

What does this mean in terms of nodered? I often use moment when I have to process a timestamp. Did I missed in terms of timestamp processing other than jsonata? Is there any plan to remove/replace moment in NR?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 December 2024 17:02 UTC](https://discourse.nodered.org/t/light-weight-library-free-time-date-string-functions-hour-min-sec-day-month-with-leading-0/93913/11 "2024-12-16T17:02:11Z")

</div>

> [@mikefila](#):
>
> What does this mean in terms of nodered?

All I know is... [JSONata : $moment -\> $dayjs - #16 by dceejay](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/16)
