# Convert 24 hour format to a 12 hour format

**URL:** https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823
**Category:** General
**Created:** [5 April 2019 23:29 UTC](https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823 "2019-04-05T23:29:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Aalian](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@Aalian](https://discourse.nodered.org/u/Aalian)
#### Post date: [5 April 2019 23:29 UTC](https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823/1 "2019-04-05T23:29:05Z")

</div>

Hello, I want to turn 24-hour format to 12-hour format. My flow goes like this. there is an interval that runs my flow at 1:00 am and makes a request to an API where it gives it some payload. the payload is formatted like this (05:28) , (21:58). Is it possible to show this in a 12-hour format like (9:45 am) or something like that?  
Thanks in advanced

---

<div class="post-metadata">

### Author: ![Aalian](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@Aalian](https://discourse.nodered.org/u/Aalian)
#### Post date: [6 April 2019 00:13 UTC](https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823/2 "2019-04-06T00:13:42Z")

</div>

Maybe this might work but I don't know how to integrate it to node red

```auto
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.text.ParseException;
class Example2
{
   public static void main(String[] args)
   {
       //Input date in String format
       String input = "15/02/2014 22:22:12";
       //Date/time pattern of input date
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       //Date/time pattern of desired output date
       DateFormat outputformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
       Date date = null;
       String output = null;
       try{
          //Conversion of input String to date
    	  date= df.parse(input);
          //old date format to new date format
    	  output = outputformat.format(date);
    	  System.out.println(output);
    	}catch(ParseException pe){
    	    pe.printStackTrace();
    	 }
   }
}

```

---

<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: [6 April 2019 04:59 UTC](https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823/3 "2019-04-06T04:59:17Z")

</div>

Note that we use `javascript`, **not** `java` in Node Red.

Easiest way would be by using locale.  
To make it portable;

```auto
function convertTime(time24) {
  var ts = time24;
  var H = +ts.substr(0, 2);
  var h = (H % 12) || 12;
  h = (h < 10)?("0"+h):h; // leading 0 at the left for 1 digit hours
  var ampm = H < 12 ? " AM" : " PM";
  ts = h + ts.substr(2, 3) + ampm;
  return ts;
}

timeInput = msg.payload // '13:01'
timeOutput = convertTime(timeInput) // '01:00 PM'

return {"payload":timeOutput};

```

Example flow

```auto
[{"id":"60287c9a.8c2964","type":"inject","z":"6d48b728.12127","name":"","topic":"","payload":"13:01","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":435,"y":200,"wires":[["46df1209.1b809c"]],"l":false},{"id":"46df1209.1b809c","type":"function","z":"6d48b728.12127","name":"","func":"function convertTime(time24) {\n var ts = time24;\n var H = +ts.substr(0, 2);\n var h = (H % 12) || 12;\n h = (h < 10)?(\"0\"+h):h; // leading 0 at the left for 1 digit hours\n var ampm = H < 12 ? \" AM\" : \" PM\";\n ts = h + ts.substr(2, 3) + ampm;\n return ts;\n}\n\ntimeInput = msg.payload // '05:28'\ntimeOutput = convertTime(timeInput) // '\n\nreturn {\"payload\":timeOutput};","outputs":1,"noerr":0,"x":495,"y":200,"wires":[["b7f459fe.df7828"]],"l":false},{"id":"b7f459fe.df7828","type":"debug","z":"6d48b728.12127","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":555,"y":200,"wires":[],"l":false}]

```

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [6 April 2019 14:19 UTC](https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823/4 "2019-04-06T14:19:48Z")

</div>

I endorse the solution give by @bakman2 . It seems a perfect solution for your use case.

Alternatively, if you want something that is more flexible and can respond better in case you change your mind and wants a different output format then one alternative could be the [node-red-contrib-moment](https://flows.nodered.org/node/node-red-contrib-moment).

If I understood correctly your data is a string and you want it formatted differently but still being a string. The contrib node is capable of parsing the input string and will deliver a string in the format you will specify.

 ![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/34cd16b0e7beb177b7896dd2453eda78078e783c.png)

![r-02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b1c44087e5d0f732579bef0aa5da2db3b0d3b8cc.png)

---

<div class="post-metadata">

### Author: ![Aalian](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@Aalian](https://discourse.nodered.org/u/Aalian)
#### Post date: [6 April 2019 15:23 UTC](https://discourse.nodered.org/t/convert-24-hour-format-to-a-12-hour-format/9823/5 "2019-04-06T15:23:17Z")

</div>

Thank you so much guys. I like the moment node too, its flexible and easy to use. Thanks
