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
Maybe this might work but I don't know how to integrate it to node red
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();
}
}
}
Note that we use javascript
, not java
in Node Red.
Easiest way would be by using locale.
To make it portable;
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
[{"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}]
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.
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.
Thank you so much guys. I like the moment node too, its flexible and easy to use. Thanks