Working with time and date, getLocalTime(), getLocalDate() example

I could not find similar examples in the official docs and thought I would share my solution. I hope it helps someone someday…

state.localTime = getLocalTime()
state.localDate = getLocalDate()

// returns simple time string in 24 hour format, format can be adjusted to include seconds "HH:mm:ss"
def getLocalTime() {
def localTime = new Date(now()).format(“HH:mm”, location.timeZone)
log.debug "localTime = $localTime"
return localTime

// returns simple date string, format can be adjusted as needed, ex “yyyy-MM-dd”, “MM-dd”, etc.
def getLocalDate() {
def localDate = new Date(now()).format(“MM-dd-yy”, location.timeZone)
log.debug "localDate = $localDate"
return localDate

1 Like

THANK YOU! I had a problem getting DST to show up right and with your help fixed it.