Click here to Skip to main content
15,499,801 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Javascript function error. Pin
Estys10-Jun-20 1:31
MemberEstys10-Jun-20 1:31 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 1:59
Memberand180y10-Jun-20 1:59 
GeneralRe: Javascript function error. Pin
Richard MacCutchan10-Jun-20 1:37
mveRichard MacCutchan10-Jun-20 1:37 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 1:58
Memberand180y10-Jun-20 1:58 
GeneralRe: Javascript function error. Pin
Richard MacCutchan10-Jun-20 2:18
mveRichard MacCutchan10-Jun-20 2:18 
AnswerRe: Javascript function error. Pin
Richard Deeming10-Jun-20 0:13
mveRichard Deeming10-Jun-20 0:13 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 0:25
Memberand180y10-Jun-20 0:25 
GeneralRe: Javascript function error. Pin
Richard Deeming10-Jun-20 2:26
mveRichard Deeming10-Jun-20 2:26 
Try something like this:
JavaScript
$(function(){
    // Convert either "HH.mm" or "HH:mm" to the number of minutes since midnight
    var parseMinutes = function(value){
        var parts = value.split(/[.:]/g);
        switch (parts.length) {
            case 1: {
                return parseFloat(parts[0]) * 60;
            }
            case 2: {
                var hours = parseFloat(parts[0]);
                var minutes = parseFloat(parts[1]);
                return (hours * 60) + minutes;
            }
            default: {
                return 0;
            }
        }
    };
    
    // Format a number of minutes as "HH:mm"
    var formatMinutes = function(minutes){
        var hours = Math.floor(minutes / 60);
        minutes %= 60;
        
        return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0');
    };
    
    var updateHoursOpen = function(){
        var openingTime = $("#opening-time").val();
        var closingTime = $("#closing-time").val();
        
        var openingMinutes = parseMinutes(openingTime);
        var closingMinutes = parseMinutes(closingTime);
        var minutesOpen = closingMinutes - openingMinutes;
        
        // If closing is before opening, the time spans midnight. Add a day's worth of minutes:
        if (minutesOpen < 0) {
            minutesOpen += 1440;
        }
        
        var hoursOpen = formatMinutes(minutesOpen);
        $("#hoursOpen").val(hoursOpen);
        return hoursOpen;
    };
    
    // Update the hours when either input is changed:
    $("#opening-time, #closing-time").keyup(updateHoursOpen);
});
Demo[^]

NB: padStart doesn't work in Internet Explorer. But since even Microsoft agree that IE isn't a browser[^], it's usually safe to ignore it these days.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Javascript function error. Pin
and180y10-Jun-20 2:55
Memberand180y10-Jun-20 2:55 
GeneralRe: Javascript function error. Pin
and180y11-Jun-20 0:06
Memberand180y11-Jun-20 0:06 
GeneralRe: Javascript function error. Pin
Richard Deeming11-Jun-20 0:30
mveRichard Deeming11-Jun-20 0:30 
GeneralRe: Javascript function error. Pin
and180y11-Jun-20 0:45
Memberand180y11-Jun-20 0:45 
JokeRe: Javascript function error. Pin
DerekT-P11-Jun-20 3:26
professionalDerekT-P11-Jun-20 3:26 
AnswerRe: Javascript function error. Pin
jkirkerx12-Jun-20 8:57
professionaljkirkerx12-Jun-20 8:57 
GeneralRe: Javascript function error. Pin
and180y12-Jun-20 9:33
Memberand180y12-Jun-20 9:33 
GeneralRe: Javascript function error. Pin
jkirkerx12-Jun-20 9:47
professionaljkirkerx12-Jun-20 9:47 
GeneralRe: Javascript function error. Pin
and180y12-Jun-20 11:48
Memberand180y12-Jun-20 11:48 
GeneralRe: Javascript function error. Pin
jkirkerx12-Jun-20 12:11
professionaljkirkerx12-Jun-20 12:11 
GeneralRe: Javascript function error. Pin
jkirkerx12-Jun-20 11:17
professionaljkirkerx12-Jun-20 11:17 
GeneralRe: Javascript function error. Pin
and180y13-Jun-20 6:57
Memberand180y13-Jun-20 6:57 
GeneralRe: Javascript function error. Pin
Richard MacCutchan13-Jun-20 7:31
mveRichard MacCutchan13-Jun-20 7:31 
GeneralRe: Javascript function error. Pin
and180y13-Jun-20 12:48
Memberand180y13-Jun-20 12:48 
GeneralRe: Javascript function error. Pin
jkirkerx13-Jun-20 8:36
professionaljkirkerx13-Jun-20 8:36 
GeneralRe: Javascript function error. Pin
and180y13-Jun-20 12:47
Memberand180y13-Jun-20 12:47 
QuestionJavaScript slide show works bad on the first page load Pin
Hypnonis8-Jun-20 21:03
MemberHypnonis8-Jun-20 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.