Formating current date / time in multiple format with multiple output and simple calculation

Hi everyone!

I've just developed a new Node-RED node called Date Injector, and I have to say, developing for Node-RED has been a real blast! This node is a bit like simpletime, but I hope it's even more versatile :wink:

With Date Injector, you can:

  • Inject the current date and time into your flows
  • Customize the format using Day.js, thanks to them.
  • Add or subtract time with units like years, days, hours, etc.
  • Support for multiple languages (English, French, Spanish, and more)

I had a great time building it, and I hope you'll find it useful in your flows! I'd love to hear your thoughts and feedback.

5 Likes

Well done @fdecourt

We have been waiting for someone to introduce day.js to Node RED.

If I may - have you considered mirroring the work of our very own @TotallyInformation and his moment node?

I am sure @TotallyInformation will contribute/add valuable ideas to make it happen?

This is not to draw attention away from this post - but you seem to be on fire with contributing to Node RED currently :pray:

@marcus-j-davies - Moment is now in maintenance mode. See their status page - Moment.js | Docs

1 Like

[quote="marcus-j-davies, post:2, topic:91149"]
Thank you for the feedback!

Yes, I looked into what @TotallyInformation offered with the Moment node, but since Moment.js is no longer being updated, I decided to move away from it. I also explored JSONata, but it didn't quite achieve what I was looking for. I considered simpletime as well, but again, it didn't fully match my requirements. That’s why I went with Day.js and developp this node.

However, I plan to incorporate some of the great ideas from @TotallyInformation into my node, especially the ability to inject dates (priority one), the option to select timezones, and other... my node is in alpha version (just did it today !)

Yup - hence why I think a day.js equivalent should be considered? :smiling_imp:

Planting seeds boys, planting seeds...

BUT - not to distract, please see the opener of this thread :innocent:

1 Like

I will try to duplicate the moment node with the day.js one !

2 Likes

The preferred protocol is to offer to update @TotallyInformation's moment node in the first instance, instead of duplicating it.. :wink:

EDIT - but I guess it wouldn't be node-red-contrib-moment if it used day.js and not moment :dizzy_face:

1 Like

Right, I'll contact him

1 Like

@fdecourt has reached out and I've offered to open up the repo to them for the creation of a new node, perhaps a dayjs node? I can help with some testing and re-publishing.

4 Likes

I think we can keep the legacy name, people generally recognise it anyway. I've suggested a new node in the same package so people get plenty of time to move over as they feel the need.

Should be easy enough to start by duplicating the existing node and re-engineering with the day.js library. Hopefully adding a few tweaks to bring the programming up to 2020's code instead of 2010's code. :grinning:

3 Likes

@fdecourt - did this node ever get published? I can't find it in Palette Manager. I am currently using node-red-contrib-dayjs to migrate from moment node but ran into an issue with it. It does not appear to work if the date is in a property that is not the top level (works with msg.date but not msg.payload.response.date)

I'm afraid not, I've no time at the moment to work on another project and nobody else has stepped forwards. I remain happy to reassign or open the existing repo to anyone who would like to take it on.

Thanks for the update. Hopefully someone can help out. Unfortunately, I don't have the required knowledge or skills.

You can also load the dayjs npm package in a function node as a module, you will have all the flexibility that you need. And you could use link-call nodes to make it reusable.

2 Likes

How are you planning on using it? What specific feature do you need?

Could node-red-contrib-simpletime help?

Just looked at it and unfortunately, while it will work for some of my use cases, it will not work for others.

  1. In order to use this to work, the date must be in msg.date and not in a structure like msg.payload.date. The specific issue I ran into with the contrib-dayjs node was that it would only work if the date was at the top level.
  2. I also use the moment node to do date offsets (add/subtract) and I don't think contrib-simpletime will do it.

Thanks for the help - it does do one thing that I need which is convert to epoch (if I figure out how to use day.js in a function node, it will also do that).

1 Like

Here is something to get you started:

[{"id":"df2446b618ca764f","type":"function","z":"640b62bc33fa1726","name":"dayjs","func":"const d = dayjs(msg.payload.date)\nmsg.payload = {}\nmsg.payload.tomorrow = d.add(1, 'day')\nmsg.payload.now1hour = d.add(1, 'hour')\nmsg.payload.yesterday = d.subtract(1, 'day')\nmsg.payload.nowFormatted = d.format('YYYY-MM-DD HH:mm:ss')\nmsg.payload.nowTime = d.format('HH:mm:ss')\nmsg.payload.nowDate = d.format('YYYY-MM-DD')\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dayjs","module":"dayjs"}],"x":1650,"y":2160,"wires":[["588b81dbc193dc89"]]},{"id":"cd0b09aefa0c893d","type":"inject","z":"640b62bc33fa1726","name":"","props":[{"p":"payload.date","v":"","vt":"date"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1510,"y":2160,"wires":[["df2446b618ca764f"]]},{"id":"588b81dbc193dc89","type":"debug","z":"640b62bc33fa1726","name":"debug 19","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1810,"y":2160,"wires":[]}]
5 Likes

Thank you so much! I thought I would have to figure out how to access the day.js library but I see that you just use it? Is it installed as part of NR?

No. It's added in the setup tab on the function node.

1 Like

I completely missed that!! That is such a neat feature (not sure if it has always been there or is new). Any chance there could be something similar for ui-template? :crossed_fingers:t3: