# Why does monaco complain about arithmetics with Date.getTime?

**URL:** https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998
**Category:** General
**Tags:** monaco
**Created:** [8 April 2022 18:26 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998 "2022-04-08T18:26:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![maxweissboeck](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxweissboeck/32/59803_2.png) [@maxweissboeck](https://discourse.nodered.org/u/maxweissboeck)
#### Post date: [8 April 2022 18:26 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/1 "2022-04-08T18:26:18Z")

</div>

This is the code

```auto
msg.payload.forEach(element => {
    // extract the elements 
    const tag = element.substring(0,2)
    const datum = element.substring(5,15)
    const text = element.substring(18, element.length)
    
    const dd = element.substring(5,7)
    const mm = element.substring(8,10) - 1
    const yyyy = element.substring(11,15)
    
    const nextDate = new Date(yyyy, mm, dd)
    const dayDiff = Math.trunc((nextDate.getTime - today.getTime) / (1000*3600*24))

```

and this is the error message I get

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d89a94edadd8087335c87f1edfc01df70eeffd79.png)

All is of type number, so why this error?

Thanks, Max

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [8 April 2022 18:31 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/2 "2022-04-08T18:31:35Z")

</div>

Shouldn't this be `nextDate.getTime()` ... calling a function?

---

<div class="post-metadata">

### Author: ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)
#### Post date: [8 April 2022 18:32 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/3 "2022-04-08T18:32:33Z")

</div>

Shouldn't `getTime` be a function not a variable?

So accessed by `getTime()`

---

<div class="post-metadata">

### Author: ![maxweissboeck](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxweissboeck/32/59803_2.png) [@maxweissboeck](https://discourse.nodered.org/u/maxweissboeck)
#### Post date: [9 April 2022 06:57 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/4 "2022-04-09T06:57:52Z")

</div>

Thank you so much - sometimes it's so easy...

To explain why this happened and the role monaco editor plays, some details.  
I enabled monaco just yesterday and the original code I hade was this (that perfectly works, but hey, I love clean code)

```auto
    const nextDate = new Date(yyyy, mm, dd)
    const dayDiff = Math.trunc((nextDate - today) / (1000*3600*24))

```

Monaco complained about it and I used the following monaco suggestion by hitting the enter key.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d167ed1550d3ceb3dd8d15b2536dd44e7b634c2.png)

Strange enough, if you choose getTime it is not inserted as method as suggested, but as variable access, so this is the outcome:

```auto
    const nextDate = new Date(yyyy, mm, dd)
    const dayDiff = Math.trunc((nextDate.getTime - today) / (1000*3600*24))

```

imho the outcome should be

```auto
    const nextDate = new Date(yyyy, mm, dd)
    const dayDiff = Math.trunc((nextDate.getTime() - today) / (1000*3600*24))

```

So monaco could do better here I guess.  
I'm an old Eclipse & Java guy and this would not happen within Eclipse, still have to learn monacos habits.

Thanks again!

---

<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: [9 April 2022 07:54 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/5 "2022-04-09T07:54:42Z")

</div>

> [@maxweissboeck](#):
>
> Strange enough, if you choose getTime it is not inserted as method as suggested, but as variable access, so this is the outcome

While it appears that way, the various cues and context should be noted. The intellisense both shows the purple box (meaning method) and the extended tip both states `method` and shows its usage with brackets.

Not certain I would want the brackets inserted for me - partly because in JavaScript, every function is an object (you can literally do `nextDate.getTime.myvariable = 12;` if you wish) and more reasonably - a common pattern in JS is to send a function as a parameter to another function (where you would not want brackets because that would call the function not pass it).

TBF, I totally understand your point but with Monaco (and VS and VScode) that's just how it works 🤷

---

<div class="post-metadata">

### Author: ![maxweissboeck](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxweissboeck/32/59803_2.png) [@maxweissboeck](https://discourse.nodered.org/u/maxweissboeck)
#### Post date: [9 April 2022 08:09 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/6 "2022-04-09T08:09:24Z")

</div>

Thanks Steve, I definitely have to learn more about JavaScript!

---

<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: [23 April 2022 08:09 UTC](https://discourse.nodered.org/t/why-does-monaco-complain-about-arithmetics-with-date-gettime/60998/7 "2022-04-23T08:09:52Z")

</div>

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