Help needed: pulling hour and minute out input time


(Chrisb) #1

So, I’m working on a program that uses schedules a job in the cron. The user inputs a time when installing and then I want to pull out the minutes and hour. Not sure how to do that or if it’s possible. This is the format that the data comes in as when a user enters a time:

2013-11-06T10:44:00.000-0500

Obviously here 10 is the hour and 44 is the minute. But how do I grab just those parts?


(Alex King) #2

You can use the timeToday function to convert it to a Date object, and then from there you could pull out the hour and minute using .format(‘h’) and d.format(‘m’). But I’d recommend using the runDaily(date, handler) function instead (which I’m not finding in the documentation for some reason - I found it in the medicine reminder SmartApp code), and pass it the date you got from timeToday(). That should also take care of any timezone issues.


(Chrisb) #3

Thanks @alexking,

I figured out how to work with timeToday, and it looks like using that will solve my timezone issue as it update the time to UTC, which I’m guessing is server time.

Not sure how to use the .format(‘h’) and d,format(‘m’) commands to get the hour and minute. Can you give me an example of how that function is used?

Thanks again.


(Alex King) #4

Sure - I think you’d do something like this -

def d = timeToday("2013-11-06T10:44:00.000-0500")
 def h = d.format('h')
 def m = d.format('m')
 log.debug h + ":" + m

Also, check out the Groovy date documentation, that’s how I found format().


(Chrisb) #5

Thanks Alex. That’s exactly what I’m looking for and works great.