# How to string format?

**URL:** https://discourse.nodered.org/t/how-to-string-format/64612
**Category:** General
**Tags:** function-node
**Created:** [2 July 2022 13:38 UTC](https://discourse.nodered.org/t/how-to-string-format/64612 "2022-07-02T13:38:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nodiaque](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@nodiaque](https://discourse.nodered.org/u/nodiaque)
#### Post date: [2 July 2022 13:38 UTC](https://discourse.nodered.org/t/how-to-string-format/64612/1 "2022-07-02T13:38:57Z")

</div>

Hello everyone,

I have a function where I need to create a simple 00:00:00 string. I received a time in decimal format (8.25, 8.50, etc...) and I want to convert to a String HH:MM:SS. I have everything, but I can't find how to have a leading 0. So when I have 8 hours or 0 min, I need to have 00 and not 0. So right now, it will print stuff like 9:30:00 and 9:0:00 instead of 09:00:00.

My current code is fairly simple:

```auto
var payload = {ontime: ""};
var msgTime = msg.payload.split(".");
var msgHour = msgTime[0];
var msgMin = msgTime[1]/100*60;
if (msgMin < 10){
msgMin = msgMin*10;
}
payload.ontime = msgHour+":"+msgMin+":00";
var newMsg = { payload:payload};
return newMsg;

```

Thanks!

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [2 July 2022 14:24 UTC](https://discourse.nodered.org/t/how-to-string-format/64612/2 "2022-07-02T14:24:03Z")

</div>

try

```auto
msgHour.toString().padStart(2,"0")

```

---

<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: [31 August 2022 14:24 UTC](https://discourse.nodered.org/t/how-to-string-format/64612/3 "2022-08-31T14:24:45Z")

</div>

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