# Monaco editor does not allow subraction of dates

**URL:** https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052
**Category:** General
**Created:** [1 November 2021 09:45 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052 "2021-11-01T09:45:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [1 November 2021 09:45 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/1 "2021-11-01T09:45:16Z")

</div>

I am not sure where to report this. When using the Monaco editor this code, in a function node, generates an error.

```auto
const d1 = new Date()
const d2 = new Date(d1.getTime() + 1000)
msg.payload = d2 - d1
return msg;

```

Hovering over d2 in the third line shows:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c97c1fe4b35932a5c9142c7a5155544fef90864.png)

---

<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 November 2021 10:05 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/2 "2021-11-01T10:05:00Z")

</div>

I've noticed such issues occasionally in VScode as well.

If you really want to get rid of it, you can force the type checker:

```auto
const d1 = /** @type {number} */ (new Date())
const d2 = /** @type {number} */ (new Date(d1.getTime() + 1000))
msg.payload = d2 - d1
return msg;

```

Note the brackets round the actual values though. There is an issue in VSCode at least where forcing the type inline doesn't work unless you include the brackets.

* * *

Alternatively, you could use `Date.parse(d1.getTime() + 1000))` (untested) which should return an integer anyway.

Or `Number(new Date())` will also work. Or even `(new Date()).getTime()` which should also return an integer.

---

<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 November 2021 10:56 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/3 "2021-11-01T10:56:18Z")

</div>

In simple terms, It's something to do with type definitions and what is supported by the subtract operator. Typically objects don't know how to be added/subtracted from one another however some objects (like `Date`) have a prototype named `valueOf` that is automatically executed by the V8 engine (under the hood) when you try to run d1+d2

As for whether Monaco should know about this or whether it is a deliberate "good housekeeping" measure - I don't know. I'll look into it some time.

Essentially, you are subtracting numbers so use valueOf...

```auto
msg.payload = d2.valueOf() - d1.valueOf()

```

... Or coerce the type using jsdoc like Julian shows.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [1 November 2021 11:02 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/4 "2021-11-01T11:02:28Z")

</div>

@TotallyInformation @Steve-Mcl, yes there are easy workarounds, I was just reporting it in case it is something that should be looked at.

---

<div class="post-metadata">

### Author: ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)
#### Post date: [2 November 2021 16:42 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/5 "2021-11-02T16:42:43Z")

</div>

> [@Colin](#):
>
> `new Date()`

I found swapping for Date.now() solved the error.

---

<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: [2 November 2021 17:01 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/6 "2021-11-02T17:01:14Z")

</div>

Yup, that would also do it! So that is at least 5 ways - gotta wonder about JavaScript sometimes!

---

<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: [2 November 2021 21:40 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/7 "2021-11-02T21:40:01Z")

</div>

And soon to be 6 with [Temporal](https://tc39.es/proposal-temporal/docs/)

---

<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: [1 January 2022 21:40 UTC](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052/8 "2022-01-01T21:40:56Z")

</div>

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