[Solved] Help - Check if date in the future has passed?

Devs,
I've hit writers block I guess. I've been pounding away at this for a few hours. It seems it should be simple to do but I keep hitting brick walls.

I want to set a state variable containing the date/time 30 minutes into the future.
When a function runs, I want to check and see if the current date/time is after that future date/time.

state.futureDate = new Date() + 30.minutes
checkDate()

def checkDate() { 
    def currentDate = new Date()
    if (state.futureDate < currentDate) {
        log.debug "The future has passed"
        return true
    }
    else {
       log.debug "The future has not arrived"
       return false
    }
}

I’m not a groovy expert, but wouldn’t your if statement need to reference “state.futureDate” instead of just “futureDate”?

Yes, sorry, that was just a forum post error. I've edited the post to show "state.futureDate < currentDate".

I figured it out finally. Sometimes when you make a post you work even harder or think in a different direction to figure it out.
This solution works with the time in the long data type format. It calculates milliseconds for time. -1 and below means the time has passed which can also be used to figure out how much time (in milliseconds) has passed if needed.

def 30min = (30 * 60000) // 30 minutes  (1,800,000 milliseconds) in the future)
state.futureDate = new Date().getTime() + 30min
checkDate()

def checkDate() { 
    def currentDate = new Date().getTime()
    def timeDiff = state?.futureDate - currentDate
    if (timeDiff < 0 || timeDiff == null) {
        log.debug "The future has passed or the future is null"
        return true
    }
    else {
       log.debug "The future has not arrived"
       return false
    }
}
4 Likes

What programmers write on their suicide notes.

5 Likes