Date time to epoch?

Using a time input type, I'm trying to subtract now(), but I can't figure out how to do it. Is there a way to convert from a time input that is, for some odd reason, stored as a string?

groovy.lang.MissingMethodException: No signature of method: java.lang.Long.minus() is applicable for argument types: (java.lang.String) values: [2024-01-28T12:40:00.000-0500]

What's the situation you are trying to use this for?

This is undoubtedly for compatibility with a certain other platform that did things that way. :slight_smile: (Beyond the fact that I think it's serialized to/from JSON so it would need to use some format like this anyway but apparently just leaves out any magic.)

To get a real Date object you can manipulate however you want, use the timeToday() method.

(Other tips: call getTime() on that Date object if you want it in epoch time like now(), or use new Date() to get now as a Date object.)

1 Like

The end goal is to have an input datetime as epoch millis. It's so much easier to deal with a Long than String or Date type. Using parse, I got Unparseable date: "2024-01-28T01:00:00.000-0500 so I figured maybe it needed to be a Date, but... no. My thought was if I could get parse to work, I could compute epoch in millis, but that was just a means tot he end, aka rabbit hole (and not sure how with leap years/seconds).

log.debug dateValue = 2024-01-28T01:00:00.000-0500
log.debug timeToday(dateValue) = Sun Jan 28 01:00:00 EST 2024
log.debug dateToTime(dateValue) = Sun Jan 28 01:00:00 EST 2024 [does the same thing as timeToday?]
log.debug (dateValue - now()) = 2024-01-28T01:00:00.000-0500 [no error, but not useful]
log.debug (timeToday(dateValue) - now()) = No signature of method: java.util.Date.minus() is applicable for argument types: (java.lang.Long) values: [1706460053212]

What I want is to get +/- millis when subtracting from now(), subtract another datetime input string, etc.

You would need timeToday(dateValue).getTime() for this to work, one of the hints above.

1 Like

Oh beautiful! I was missing the getTIme() part. Thank you.

log.debug (timeToday(dateValue).getTime() - now()) = -39541871 [exactly what I wanted!]

1 Like

Here's one example - While fixing something in Advanced Thermostat yesterday I was working in this area - Take Note of line 179, 196 and the math on 198

That is essentially what I'm doing too.

Line 179, new Date().getTime() is the same thing as now()?