Conversion of date value to number (16:08:00 => 58,080s)


I need to convert time from DATE format to NUMBER format (number of seconds). I will try to explain with a picture (the image is simulated using input).

Please help create a code that:

  • convert DATE to STRING (this works: var sunrise={"payload":msg.start.toString()};)
  • split STRING into hour, minute, second (this works too: var V_hour={"payload" :vychod.payload.slice(16, 18)})
  • convert STRING to NUMBER
  • multiply the hours and minutes to reach the second
  • addition

Thank you for your help

Welcome to the forum @FrantaD.

You can convert a javascript Date object to a milliseconds timestamp using
let sunrise = theDate.getTime()
It looks as if you are trying to get the number of seconds since the start of the day, in which case you first need to get the number of ms since the start of the day, by taking the modulus with 24 hours in ms
sunrise = sunrise % (24 * 3600000)
Then divide by 1000 to get seconds.

However, that seems a slightly unusual thing to need, can you explain why you need that?

Hi Colin,

That's exactly what I was looking for. Even better :slight_smile:

And the reason for this transfer is the transfer of this information to the PLC via EtherNet/IP communication.

Thank you!

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