# $moment() function in jsonata

**URL:** <https://discourse.nodered.org/t/moment-function-in-jsonata/41220>\
**Category:** General\
**Created:** [18 February 2021 09:14 UTC](https://discourse.nodered.org/t/moment-function-in-jsonata/41220 "2021-02-18T09:14:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nileio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nileio/32/9444_2.png) [@nileio](https://discourse.nodered.org/u/nileio)\
**Post date:** [18 February 2021 09:14 UTC](https://discourse.nodered.org/t/moment-function-in-jsonata/41220/1 "2021-02-18T09:14:03Z")

</div>

Hi all,  
I recently discovered that `$moment()` function is available in Jsonata expression.This is great to have it there.I stumbled upon an issue though and hope you may lend me a hand.

I am trying to subtract a variable duration from a date. The function signature in momentjs should be  
`moment(date).subtract(value,string).format("x")`

when I do the same thing in Jsonata expression I cannot provide the chained function , that is `subtract` any variables. It works only if I hard coded the values  
that is it does **not** work if I try  
`$moment(msg.payload).subtract(msg.somevalue,msg.aduration).format("x")`

it works only if  
`$moment(msg.payload).subtract(1,"w").format("x")`

so my question is : is it something I am doing wrong, or is it that chained methods are unsupported by the Jsonata function in node-red?  
I am not really sure how does node-red add functions to the Jsonata library of functions..  
@janvda  
thank you

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 February 2021 11:08 UTC](https://discourse.nodered.org/t/moment-function-in-jsonata/41220/2 "2021-02-18T11:08:37Z")

</div>

Hi @nileio

I think this is due to the way jsonata references message properties.

I think you'll find this works:

```auto
$moment($.payload).subtract($$.somevalue,$$.aduration).format("x")

```

---

<div class="post-metadata">

**Author:** ![nileio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nileio/32/9444_2.png) [@nileio](https://discourse.nodered.org/u/nileio)\
**Post date:** [18 February 2021 11:17 UTC](https://discourse.nodered.org/t/moment-function-in-jsonata/41220/3 "2021-02-18T11:17:47Z")

</div>

> [@knolleary](#):
>
> I think this is due to the way jsonata references message properties.
> 
> I think you'll find this works:
> 
> ```auto
> $moment($.payload).subtract($.somevalue,$.aduration).format("x")
> 
> ```

did not even cross my mind and indeed it is the solution! it worked beautifully.  
Thanks for help @knolleary

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 March 2021 11:18 UTC](https://discourse.nodered.org/t/moment-function-in-jsonata/41220/4 "2021-03-04T11:18:11Z")

</div>

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