Convert string to time and handling null values

Hello,;

I'm trying to create an app where the user enters the time they wish for a certain event to happen.  They can enter up to 5 events which will be stored in an array in which the next occurring time is picked.  They will enter it in a 24-hour format (00:00 to 23:59) in a string, and this will be compared against the current time to trigger the next event to take place.

However I'm coming up with two major problems - how to deal with null values (I thought I did but it's not working) and the values are either not converting to time correctly or not able to compare with the current time.  I need to work with null values because the user does not need to fill all the times.

My code is as follows:-

function convertHour(timeUnit) {
    var timeDisplay = timeUnit;
    var timeHr = timeUnit.substring(0,2).toNumber();
    var timeMin = timeUnit.substring(3,5).toNumber();
    if (!System.getDeviceSettings().is24Hour) {
        if (timeDisplay != null) {
        timeHr = timeHr % 12 ? timeHr % 12 : 12;
        timeHr = timeHr.format("%02d");
        timeMin = timeMin.format("%02d");
        timeDisplay = Lang.format("$1$:$2$", [timeHr, timeMin]);}
        } else {
        timeHr = timeHr.format("%02d");
        timeMin = timeMin.format("%02d");
        timeDisplay = Lang.format("$1$:$2$", [timeHr, timeMin]);
        }
    return timeDisplay;
}

function onUpdate(dc) {

var currentTime = Lang.format(timeFormat, [hours.format("%02d"), mins.format("%02d")]);
var timeArray = [convertHour(time01), convertHour(time02), convertHour(time03), convertHour(time04), convertHour(time05)];

var nextTime = timeArray[0]; 
    for (var i=1; i < timeArray.size(); i++) { 
        if (timeArray[i] > currentTime) { 
        nextTime = timeArray[i]; 
        } 
    }
}

Please advise.

  • I'm sorry, I do apologise having to ask all these questions.  I normally only use the forums as a last resort once I've spent ages exhausting all options.  I've managed to turn the values into numbers and all is well now.  Thanks for your help.