Tag Archives: Node-Red global date variable

Handy Node-Red Date

While working on my thermostat I found myself repeating the same code all over the place – as I use function nodes for inline debugging and info – far more useful at times than the debug output.

I’ve now attached this simple code here to a function in my init page. I can use the global var “handyDate” all over the place as many times as required. This function is triggered by an inject node every second. I could have converted the timestamp info from the trigger but this is just as easy.

    var date = new Date();

    var hour = date.getHours();
    hour = (hour < 10 ? "0" : "") + hour;

    var min  = date.getMinutes();
    min = (min < 10 ? "0" : "") + min;

    var sec  = date.getSeconds();
    sec = (sec < 10 ? "0" : "") + sec;

    var year = date.getFullYear();

    var month = date.getMonth() + 1;
    month = (month < 10 ? "0" : "") + month;

    var day  = date.getDate();
    day = (day < 10 ? "0" : "") + day;

global.set("handyDate"," at " + hour + ":" + min + ":" + sec + " on " + day + "/" + month + "/" + year);

Facebooktwitterpinterestlinkedin