[SOLVED]I need to convert date and time format

I need your help, because i'm struggling with formatting date and time

The situation looks that I need year, month, day, hour and minute and I want to use node-red-contrib-iiot-ntptime for taking date and time. I need present those values as binary numbers. For example:
-actual year: 2023, binary 0001 0111 (eliminate "20");
-actual month: 04, binary 0100 0000;
-actual day: 09, binary 0000 1001;
-actual hour: 18, binary 0001 0010;
-actual minute: 45, binary 0010 1101

Now I need to combine year+month, getting 0100 0000 0001 0111;
day+hour: 0000 1001 0001 0010;
minute: 0010 1101 0000 0000

At the end, these binary numbers are to be converted to decimal, so it should be 16407, 2322 and 11520.

I know it's complicated, but I have tried to describe the problem simply and in detail and hope someone will help me. Also I've tried do my own way, but all I can do is take values from ntp time and convering it into binary.

Greetings

Has me slightly confused.
This may help.

let year = "2023", month = "04", day = "09", hour = "18", minute ="45";
let payload = `${year}-${month}-${day}T${hour}:${minute}Z`
const date_obj = new Date(payload);
let date_array = [
    (date_obj.getUTCFullYear() - 2000).toString(2).padStart(8, "0"),
    (date_obj.getUTCMonth() + 1).toString(2).padStart(4, "0").padEnd(8, "0"),
    date_obj.getUTCDate().toString(2).padStart(8, "0"),
    date_obj.getUTCHours().toString(2).padStart(8, "0"),
    date_obj.getUTCMinutes().toString(2).padStart(8, "0")  
]
node.warn(date_array);
msg.payload = {
    "year_month":parseInt(`${date_array[1]}${date_array[0]}`, 2),
    "day_hour":parseInt(`${date_array[2]}${date_array[3]}`, 2),
    "minute":parseInt(`${date_array[4]}00000000`, 2)
}
return msg;


[edit] fixed typo.

2 Likes

Big thanks to you, it's working well!

Greetings!

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