How to convert 'time' input to epoch time

Having some trouble getting the 'time' input converted to epoch time in an app so I can more easy do some math with the time.
Per Docs: * time: dateTime String (formatted "yyyy-MM-dd'T'HH:mm:ss.sssXX"); this pulls up a time picker.

inputTime = "2025-06-01T19:29:00.000-0700" // The value I see if I read the input
timeX = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", inputTime)
epochTime = timeX.getTime()

But it wont parse. Any ideas on how to get the 'time' input converted to epoch?

Did you try using the same string as what is shown as the format for the input?

timeX = Date.parse("yyyy-MM-dd'T'HH:mm:ss.sssXX", inputTime)

XX can be the time zone offset, -0700 in your example, including the - symbol

Yeah, tried that to. Still parse error

I found this post example that uses SimpleDateFormat to format the inputTime before doing the parse.

Edit: I should have looked closer at that, it appears to be an example of code that does not work, at least if you run it on that page. Still, SimpleDateFormat may be what you are looking for.

Edit2: Or ask AI, this is what I got:

Method 1: Using now()

def currentEpoch = now()

Method 2: Using System.currentTimeMillis()

def currentEpoch = System.currentTimeMillis() / 1000 // Convert milliseconds to seconds

Method 3: Using new Date()

def currentEpoch = (new Date().time / 1000) as Long

Don’t know if this is same situation but here’s a scenario I ran into recently RM 5.1 Convert a string to time variable? - #7 by kenrok1 that may also help

This works, I just tested it

import java.text.SimpleDateFormat

void someFunction() {
    def inputTime = "2025-06-01T19:29:00.000-0700" // The value I see if I read the input
    def timeX = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssXX").parse(inputTime)
    def epochTime = timeX.getTime()
    log.warn "${epochTime}"
}

Thanks for all thee suggestions which helped troubleshoot this. I figured it out. My function has 2 different possible date formats and getTime() is only compatible with one of them.

Sunrise/Sunset is using: "EEE MMM dd HH:mm:ss zzz yyyy" this works with getTime()
input Time is using "yyyy-MM-dd'T'HH:mm:ss.sssXX"

Solution was to parse all into the same compatible format then use getTime()

2 Likes

Why would you not use any of the one-liners I posted instead of parsing a date still?

now() is all you need.

1 Like

Don't forget about the built-in Hubitat method timeToday(), which was created to help with this kind of conversion from "time" inputs. For example:

// Assumes input name is `yourSettingName`; returns a `Date`:
def aDateTime = = timeToday(yourSettingName, location.timeZone)
// Convert to epoch time:
def epochTime = aDateTime.getTime()

Sounds like you already have something, but just wanted to point that out. :slight_smile: And since you mentioned sunrinse/sunset, note that the built-in getSunriseAndSunset() method will return a Map with Dates as values, so the same methods should work on those if that's what you're using to get that data (although if you're just comparing the two, you don't really need to drop down to epoch time since Java/Groovy and Hubitat have methods to compare the two; and also note that Hubitat provides a timeOfDayIsBetween(start, stop, theTime, timeZone=null) method if that's what you're doing.)

4 Likes