[node-red-contrib-simpletime]mydate

@Paul-Reed (I'm assuming this is yours but maybe there are two paul-reeds :)) )

I LOVE this node and use it a lot as a method of inline debugging timestamping :slight_smile:

But I also use it to add mydate to tweets and I wondering if it could be tweaked slightly?

It currently looks like

Thu Sep 06 2018

and I was thinking that it might look better as

Thu 6 Sep 2018

Its no big deal at all but what do you think?

Simon

I thought you were out of suggestions for September ? :slight_smile:

1 Like

Only for core :slight_smile:

carry on !

1 Like

Looks like he has the date in the format used in the US. Why not add a RFE or PR to the node asking for an option to specify the format? Of course then it will be the 'alittlelessthansimpletime' node :stuck_out_tongue_winking_eye:

aah - fair enough then :slight_smile:

I wouldn't want to do that as I too, don't want it to be "alittlelessthansimple' node :slight_smile:

I would have preferred to see the date formatted Thu 6 Sep 2018, but as the node obtains the date by using;

var d = new Date();
msg.mydate = d.toDateString();

..which returns Thu Sep 06 2018 as it's the default format returned by javascript.

But it can be done, try importing the attached flow, and let me know what you think.

Paul

[
    {
        "id": "8118cf57.cbf43",
        "type": "function",
        "z": "f82b93ba.98aad",
        "name": "simpletime",
        "func": "const monthNames = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"June\", \"July\", \"Aug\", \"Sept\", \"Oct\", \"Nov\", \"Dec\"];\nconst dayNames =[\"Sun\", \"Mon\", \"Tues\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"];\nvar d = new Date();\n        dt = d.toDateString();\n        e = d.getTime();\n        h = (d.getHours());\n        m = (d.getMinutes());\n        mn = (monthNames[d.getMonth()]);\n        dy = (dayNames[d.getDay()]);\n        dt = d.getDate();\n        yr = d.getFullYear();\n        \n        if (m < 10) {\n           var min = '0' + m;\n           }\n           else (min = m);\n        s = (d.getSeconds());\n        if (s < 10) {\n           var sec = '0' + s;\n           }\n           else (sec = s);\n        var tm = (h+\":\"+min);\n        var tms = (h+\":\"+min+\":\"+sec);\n        var ms = (min+\":\"+sec);\n        var sd = (dy+\", \"+dt+\" \"+mn+\" \"+yr);\n        \n        msg.myrawdate = d;\n        msg.myepoch = e;\n        msg.mysecond = s;\n        msg.myminute = m;\n        msg.myminutes = ms;\n        msg.myhour = h;\n        msg.mytime = tm;\n        msg.mytimes = tms;\n        msg.mydate = sd;\n\n        node.send(msg);\n    ",
        "outputs": 1,
        "noerr": 0,
        "x": 440,
        "y": 390,
        "wires": [
            [
                "4540b623.bebb58"
            ]
        ]
    }
]

Your flow is fine

I made one in Blockly to do very similar while I was waiting around this afternoon :slight_smile:

So, are you up for rolling that into your contrib-node itself? (More than willing to do the coding changes and issue a pull-request on your GitHub repository if your busy with other stuff)

Simon

Sorry if I kept you waiting Simon!
Aahh, the benefits of being semi-retired :sunglasses::sunglasses::sunglasses:

Paul

1 Like

I can do the update, but let's see if there is other feedback which we may need to consider before doing so.

Paul

1 Like

Patience is my middle name :slight_smile:

1 Like

Hi, Please could someone share any flow examples they have that use simpletime, I tried it a short while ago - to I think convert an epoch/unix time into something human readable - but I could not get it to work. :frowning:

I think that this is an issue where it depends on where in the world and how you normally expect the date to look. I'm in the US and would want it in Month, Day Year because that is the convention I am use to. Others will want it Day, Month year.

So unless you give people the option to choose the format they want, any change will pelase some and sadden others who may request the opposite in a PR in the future.

My two cents

It seems that using d.toLocaleDateString() instead of d.toDateString() ought to help, but I'm stuck in a place where it makes no difference, and I've had no luck experimenting with locales and options arguments inside a function node. In any case, I think the simple solution is to get the format that is customary where you live and use something alittlelessthansimple if you want different.

[{"id":"c4d7e0f.675742","type":"inject","z":"c84eeaca.be45c8","name":"","topic":"","payload":"red","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":360,"wires":[["d957d526.acd958"]]},{"id":"f6ebc6c6.aa2c08","type":"debug","z":"c84eeaca.be45c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":850,"y":360,"wires":[]},{"id":"d957d526.acd958","type":"simpletime","z":"c84eeaca.be45c8","name":"","x":350,"y":360,"wires":[["12b6c2ca.78732d"]]},{"id":"12b6c2ca.78732d","type":"change","z":"c84eeaca.be45c8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"Colour is \" & payload & \" and the date is \" & mydate","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":360,"wires":[["f6ebc6c6.aa2c08"]]}]

Unfortunately not ... The node.js binary is not built by default with all the locale awareness needed for toLocaleString to do the right thing.

Thanks, Nick. I can rest now. Not a big fan of locales anyway but a heavy user of the moment node.

Ah yes, been there, been burned by that... but the node-red-contrib-moment node is .
Simon, you can get the date format you want by specifying the format ddd D MMM YYYY

Here is an example flow:

[{"id":"3ad33994.9a7d56","type":"inject","z":"58c8eb7a.5496c4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":860,"y":2920,"wires":[["f7be760e.adf2e8"]]},{"id":"f7be760e.adf2e8","type":"moment","z":"58c8eb7a.5496c4","name":"","topic":"","input":"","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"ddd D MMM YYYY","locale":"en_US","output":"","outputType":"msg","outTz":"America/New_York","x":980,"y":2960,"wires":[["b295243a.29e318"]]},{"id":"b295243a.29e318","type":"debug","z":"58c8eb7a.5496c4","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":1100,"y":3000,"wires":[]}]

(sorry, I know it doesn't actually answer the question about how to use simpletime...)

1 Like

Thanks :slight_smile:

No forum question ever survives the OP :slight_smile:

But good to know there is the nuclear option if required
I just wanted the day of the month before the name of month and drop the leading zero ........... :slight_smile:

In all fairness the current

www mmm dd yyyy

format is a good computer choice as its easily parsed using sub-strings to produce anything anyone wants

www, [d]d mmm[m] yyyy

isn't quite so easy (could still be parsed by using split by space though)