How to subtract minutes from time

This should be simple, but for the life of me I cannot find a documented way to do it that works.

I have a date/time for an alarm, and I want to subtract some number of minutes from it. Its for a sunrise alarm that slowly dims from 0 to 100.

The inputs for the app are:

input alarmTime, time, title: Alarm Time, submitOnChange: true, required: true
input alarmDuration, number, title: Alarm Duration (minutes), defaultValue: 30, required: false, range: 15..60, submitOnChange: true

And later in my code I would think I should be able to do:
def adjustedAlarmTime = alarmTime - (alarmDuration * 60000)

But no success, what am I doing wrong here?

I recently added a time input to an app and you have you convert it to a date type variable so you can then use date functions:
def dateTime = Date.parse("yyyy-MM-dd'T'HH:mm:ss.sssXX", settings.alarmTime).getTime()

The getTime at the end gets an epoch time in seconds so you can add time to it by multiplying your second input by 60:
newDateTime = dateTime + (settings.alarmDuration * 60)

Dates are always a challenge to code around and I find using epoch the easiest but am sure there is a way to add minutes directly to a date object.

1 Like