Why does monaco complain about arithmetics with Date.getTime?

This is the code

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

All is of type number, so why this error?

Thanks, Max

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

Shouldn't getTime be a function not a variable?

So accessed by getTime()

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)

    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.

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

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

imho the outcome should be

    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!

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 :person_shrugging:

1 Like

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

1 Like

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