# JSONata : $moment -\> $dayjs

**URL:** <https://discourse.nodered.org/t/jsonata-moment-dayjs/90795>\
**Category:** Core Development\
**Tags:** dayjs, moment\
**Created:** [8 September 2024 20:17 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795 "2024-09-08T20:17:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [8 September 2024 20:17 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/1 "2024-09-08T20:17:05Z")

</div>

During a recent conversation, @dceejay brought up that moment is no longer suggested.  
Doing my own research, this certainly seems to be the case.

So I experimented, and I was able to add `dayjs` to the engine with little to no stress.

```auto
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");
dayjs.extend(utc);
dayjs.extend(timezone);

....

expr.assign('dayjs', function(arg1, arg2, arg3, arg4) {
        return dayjs(arg1, arg2, arg3, arg4)
});

```

 ![Screenshot 2024-09-08 at 21.14.01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/28771b0ea8c61e2f356dddbb2eaa938999c4a8f2.png)

Is it of sound mind to add this in the next feature update?  
to path the way of moving away from moment?

The API's are almost identical, adding it now - can open up the door to removing moment in a future major?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 September 2024 20:30 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/2 "2024-09-08T20:30:27Z")

</div>

I've used dayjs previously in node-RED, and found it quite easy to use.  
I particularly like it because it's just 2kB in size.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [8 September 2024 20:35 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/3 "2024-09-08T20:35:55Z")

</div>

I use it quite a bit, and become familiar with it fast, given the API is based on that from moment.

To me at least, its a no brainer to start the transition to dayjs, get it in early, allow time for the public to adapt, before removing moment in v5

---

<div class="post-metadata">

**Author:** ![dangruhn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dangruhn/32/91097_2.png) [@dangruhn](https://discourse.nodered.org/u/dangruhn)\
**Post date:** [28 September 2024 21:16 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/4 "2024-09-28T21:16:51Z")

</div>

I have been able to use dayjs by adding

```auto
    functionGlobalContext: {
...
        dayjs: require('dayjs')
    },

```

to settings.js  
However, I cannot access utc and timezone. What does it mean to add this code "to the engine"?

> ```auto
> const dayjs = require("dayjs");
> const utc = require("dayjs/plugin/utc");
> const timezone = require("dayjs/plugin/timezone");
> dayjs.extend(utc);
> dayjs.extend(timezone);
> 
> ```

(In general, I'm still trying to understand adding packages with plugins to Node-RED. If anyone can point me to a document and/or tutorial I'd greatly appreciate it.)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 September 2024 22:26 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/5 "2024-09-28T22:26:07Z")

</div>

You just add dayjs to the setup tab of a function and then access it.. that's it. No `require` needed.

> **[Use any npm module in Node-RED • FlowFuse](https://flowfuse.com/blog/2023/06/import-modules/)**
>
> Node-RED has an incredibly rich resource of integrations available, but sometimes you need that little extra. This shows you how.

---

<div class="post-metadata">

**Author:** ![zba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zba/32/91182_2.png) [@zba](https://discourse.nodered.org/u/zba)\
**Post date:** [29 September 2024 06:33 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/6 "2024-09-29T06:33:27Z")

</div>

> you just add dayjs to the setup tab of a function and then access it..

I don't think it would work for `change` node, OP show.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 September 2024 06:40 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/7 "2024-09-29T06:40:17Z")

</div>

I missed the beginning of the thread and answered you as if you were doing the code in a function node since you mentioned `functionGlobalContext` which is the old method of gaining access to modules in a function (which is why I pointed you to the article)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [29 September 2024 19:55 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/8 "2024-09-29T19:55:44Z")

</div>

Given this thread received a heartbeat!  
I’m eager to know if the core team have a view here?

I’m happy to put up a PR to make it happen? (Along side $moment of course)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 September 2024 21:42 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/9 "2024-09-29T21:42:07Z")

</div>

Yes. I think moving towards day.js makes sense, as at some point moment will get deprecared

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 September 2024 08:11 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/10 "2024-09-30T08:11:06Z")

</div>

> [@marcus-j-davies](#):
>
> I’m happy to put up a PR to make it happen? (Along side $moment of course)

Happy for the PR but "alongside"? Bah! This is just more code to maintain. We are not great at depreciating things.

We may wish to explore introducing it via a setting that exclusively choses one or the other (not both) with the aim to remove Moment at some future point.

An alternative approach could be to drop moment and mimic its functionality using dayjs? for example `moment.isMoment` could be mapped to call `dayJs.isDayjs` however i am not 100% certain all functionality of moment can be so easily proxied to dayjs functionality.

Other ideas and thoughts welcome of course.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 September 2024 08:18 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/11 "2024-09-30T08:18:06Z")

</div>

You could always mark it as about to be deprecated, then set a future version as the endpoint. Probably should be quite a while ahead though as some folk don't seem very good and updates. 🙂

If Moment could be transparently replaced though, I guess that would be even better.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 September 2024 08:25 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/12 "2024-09-30T08:25:23Z")

</div>

My plan was going to be add `$dayjs()` to the list (not touching `$moment()`)  
and for the project to start favouring `day.js` over `moment` - in view of it being removed in the future.

if I may @Steve-Mcl I'll PR the dayjs addition as the foundation?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 September 2024 08:28 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/13 "2024-09-30T08:28:46Z")

</div>

> [@marcus-j-davies](#):
>
> if I may @Steve-Mcl I'll PR the dayjs addition as the foundation?

There is no reason you cannot make a start TBF, but I would personally be against duplication without a clear path to final migration. Something agreed with clear outlines.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 September 2024 08:39 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/14 "2024-09-30T08:39:07Z")

</div>

Here is my suggestion then. If you can squeeze dayjs in fairly quickly. Mark $moment for removal in Node-RED v5 as a potentially breaking change.

Maybe someone can even work out a way to automate a migration as well.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 September 2024 10:21 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/15 "2024-09-30T10:21:11Z")

</div>

Initial PR raised.

> <https://github.com/node-red/node-red/pull/4896>
>
> \## Types of changes
> 
> 
> 
> \- \[\] Bugfix (non-breaking change which fixes an issu…e)
> \- \[x\] New feature (non-breaking change which adds functionality)
> 
> 
> 
> \## Proposed changes
> 
> As discussed on the forum : https://discourse.nodered.org/t/jsonata-moment-dayjs
> 
> This PR introduces \`$dayjs\` to the expressions engine.
> 
> !\[Screenshot 2024-09-30 at 11 08 06\](https://github.com/user-attachments/assets/14963a06-90ba-409c-ba52-fab838d79504)
> 
> !\[Screenshot 2024-09-30 at 11 08 51\](https://github.com/user-attachments/assets/90f5e867-d797-4ea5-87bc-bd4353f63df6)
> 
> My view here, is to move over to the modern/smaller equivalent of \`$moment\`
> According to my research, many have transitioned from, or have removed support for \`moment\`, and switched to \`dayjs\`
> 
> I haven't done anything to \`$moment\`, as I think this PR should represent a move away from it.
> Happy to discuss any hurdles that may not be seen by myself.
> 
> 
> \## Checklist
> 
> \- \[x\] I have read the \[contribution guidelines\](https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md)
> \- \[x\] For non-bugfix PRs, I have discussed this change on the forum/slack team.
> \- \[x\] I have run \`npm run test\` to verify the unit tests pass
> \- \[\] I have added suitable unit tests to cover the new/changed functionality

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 September 2024 12:32 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/16 "2024-09-30T12:32:26Z")

</div>

Given the tiny library size of dayjs I wouldn't be worried about having both for a while. I think we should then announce that moment will be deprecated in Node-RED v5 and removed in v6.

---

<div class="post-metadata">

**Author:** ![dangruhn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dangruhn/32/91097_2.png) [@dangruhn](https://discourse.nodered.org/u/dangruhn)\
**Post date:** [30 September 2024 14:21 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/17 "2024-09-30T14:21:03Z")

</div>

Thanks, @Steve-Mcl.

Here is what I found. This goes in the Setup tab of a function node:

 ![{3C87AFB2-BE62-49DE-8196-B3F37F3C2491}](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/eae3705fa385caf16468bd1e19c2190a42c75125.png)

Then, use this code:

```auto
dayjs.extend(utc);
dayjs.extend(timezone);

```

I would fully support adding dayjs as a standard part of Node-RED. It takes a little adjustment, but since moment.js is deprecated, it is time to start moving on to something else.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [1 October 2024 18:57 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/18 "2024-10-01T18:57:45Z")

</div>

I use the moment node alot. Does this mean that node will stop working eventually?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 October 2024 19:01 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/19 "2024-10-01T19:01:02Z")

</div>

> [@gerry](#):
>
> moment node

This is about built-in moment. The moment node will be unaffected.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 October 2024 20:40 UTC](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795/20 "2024-10-01T20:40:01Z")

</div>

> [@gerry](#):
>
> Does this mean that node will stop working eventually?

It will eventually. But I wouldn't worry too much, I'm certainly not about to get rid of it and it looks like the MomentJS library isn't in a hurry to die either. There are rather a lot of other projects that still rely on it.

However, good news is that @fdecourt is working on a new node using DayJS that mirrors my old moment node as closely as possible. Hopefully it will be easy to switch over.

[Next page](https://discourse.nodered.org/t/jsonata-moment-dayjs/90795.md?page=2)
