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.

  • You use timeUnit as a string before verifying it's not null.

    You add null values to the array but don't test the array values for not being null.

    I'd only add the time to the array if it isn't null.

    I'd also create an array rather than having timeHr and timeMin (which are used to create an array).

    (The "convertHour" name is weak. It's converting to time not to hours.)

  • The parsing probably needs to be a bit smarter, as a user could enter 1:05 instead of 01:05, or 25:05, or 01:75, or 0105.

    I'd start by looking for the ":" if it's non-null, then dealing with hour and minute separately.  The whole string could be null or parts could be.  Consider just

    01:

  • Sure, there are other issues too. But we probably don't want to teach how to program (which is where this appears to be going).

    Substring won't work on a null value.

    Lines 3 and 4 need to be moved to the block after line 6 as a start.

    I'd probably make users just enter a 3 or 4 digit number for 24h time. (Rather than dealing with the ":", which will be annoying to users to have to enter.)

    "1315" for 1:15pm and "315" for 3:15am for example.

  • The users will be clearly instructed to enter the time in the "00:00" format, so any such errors would be in the way they enter it.  Then I will take the first and second characters and convert to number as hours, then the fourth and fifth as minutes, leaving the colon.  However I could try removing the colon and using just the four digits as further suggested.

  • I can try getting them to add four digits even with the zero as a standard format and see how that goes.

  • You'll soon learn that you're lucky if some users even read the first line of a description!  Slight smile

    Plus, there are typos....

    I'm not sure a leading zero is that common, at least in the US.

  • Users (often) don't read instructions.

    They will, though, leave you bad reviews when they make your app crash.

    ===============

    Read the string as one number (of any number of digits through 4).

    You can then use mod to get the minutes and the use that to get the hours.

  • Plus, there are typos....

    I'm not sure a leading zero is that common, at least in the US.

    He should be using a number picker (that's one reason to not have the user enter the colon). That avoids the typo issue.

    There's no reason, either, to require the user to enter the leading zero. It's easier for the user not to have to enter it and it's no work to write the code to handle the user not entering it.

    The user would have to remember to use 24h time (not common in the US), but that avoids having to enter some sort of a!/on indicator).

  • Not sure if this is entered on the watch or with app settings.  If it's a string, I'm thinking app settings.

    I'd probably go with some kind of am/pm indicator (could be a boolean in app settings that's ignored if the watch is in 24hr mode.)  If you say "13:45" to someone in the US, it takes a while to convert!   But not as as as KM to miles! Slight smile

    As far as users reading, in one of my widgets, the first line of description is:

    !!!!!NOTE!!!!! !!!!!PLEASE READ!!!!! !!!!!NOTE!!!!

    and it's amazing you many people don't read the very next line and leave a bad review!  Slight smile

  • Not sure if this is entered on the watch or with app settings.  If it's a string, I'm thinking app settings.

    There is that. Using settings is kind of fussy. It's not the first thing I think of with "the user enters the time".

    If it's settings, the app would have to be smarter about the string to not be annoying. It would need to be clear about strings it doesn't like.

    I'd use a "p" or "a" in the string for non-24h numbers.

    It's not that hard for people to convert 24h time and it's a place to start that would make the app flexible and reliable.

    He can add fancy stuff later.