Calculate with collection field TIME and timepicker

Hey community,

I have two timepicker items, connected to a collection TIME field on my site, and I wonder if and how I can do some calculations with that?

  1. I want to achive: timepicker2.value (-) timepicker1.value
  2. timepicker2.value = timepicker1.value (+) 30 min

One of my ideas was to extract numbers and return a string but I can’t set the timepicker2 with that string! Please Help!!!

export async function EndZeit(Anfangszeit) {
let Endzeit
let HourValue1 = await Number(Anfangszeit.substr(0, 2));
let MinuteValue1 = await Number(Anfangszeit.substr(3, 2));
if (MinuteValue1 === 30) {
return Endzeit = HourValue1 + 1 +“:00”
} else {
return Endzeit = HourValue1 + “:” + “30”
}
}

Anfangszeit: 09:00:00.000
Endzeit: 9:30

I couldn’t understand what exactly you wish to achieve (maybe try to add clearer explanation),
Anyway 2 comments:
To get the hours and minutes you can use:

let timeSplitter = timeValue.split(":");
let hours = Number(timeSplitter[0]);
let minuets = Number(timeSplitter[1]);

Also you don’t need an async function and await here, as Number() is not a promise.

Thanks J.D. for your quick feedback.
Maybe my thinking is to complicated!

What is the easiest way to add 15 min to $w(‘#timePicker1’).value and display it to $w(‘#timePicker2’).value?

Maybe something like this (I didn’t test it):

let timeSplitter = $w("#timePicker1").value.split(":");
let hour = Number(timeSplitter[0]) ;
let sumMinutes = Number(timeSplitter[1]) + 15;
let additionaHours = Math.floor(sumMinutes/60);
let newHour = ((additionaHours  + hour) % 24).toString();
let newMinutes = (sumMinutes % 60).toString();
$w("#timePicker2").value = newHour.padStart(2,'0')+ ":"  + newMinutes.padStart(2,'0');

(UPDATED (forgot Math.floor())

Updated this line to support date change:

let newHour = ((additionaHours  + hour) % 24).toString();

Oh this is just awesome working! Thanks a lot J.D. you realy saved my day!!!

you’re welcome :slight_smile: