Click here to Skip to main content
15,884,177 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Data from one page to another with a twist. Pin
and180y15-Jun-20 1:24
and180y15-Jun-20 1:24 
GeneralRe: Data from one page to another with a twist. Pin
Richard Deeming15-Jun-20 1:27
mveRichard Deeming15-Jun-20 1:27 
GeneralRe: Data from one page to another with a twist. Pin
and180y15-Jun-20 1:46
and180y15-Jun-20 1:46 
GeneralRe: Data from one page to another with a twist. Pin
Richard Deeming15-Jun-20 4:59
mveRichard Deeming15-Jun-20 4:59 
GeneralRe: Data from one page to another with a twist. Pin
and180y15-Jun-20 8:56
and180y15-Jun-20 8:56 
GeneralRe: Data from one page to another with a twist. Pin
Richard Deeming15-Jun-20 8:58
mveRichard Deeming15-Jun-20 8:58 
GeneralRe: Data from one page to another with a twist. Pin
and180y15-Jun-20 9:07
and180y15-Jun-20 9:07 
GeneralRe: Data from one page to another with a twist. Pin
Richard Deeming15-Jun-20 9:16
mveRichard Deeming15-Jun-20 9:16 
QuestionJavascript function error. Pin
and180y9-Jun-20 13:55
and180y9-Jun-20 13:55 
AnswerRe: Javascript function error. Pin
Richard MacCutchan9-Jun-20 21:03
mveRichard MacCutchan9-Jun-20 21:03 
GeneralRe: Javascript function error. Pin
and180y9-Jun-20 23:27
and180y9-Jun-20 23:27 
GeneralRe: Javascript function error. Pin
Richard MacCutchan9-Jun-20 23:36
mveRichard MacCutchan9-Jun-20 23:36 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 0:21
and180y10-Jun-20 0:21 
GeneralRe: Javascript function error. Pin
Estys10-Jun-20 0:31
Estys10-Jun-20 0:31 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 0:59
and180y10-Jun-20 0:59 
GeneralRe: Javascript function error. Pin
Richard MacCutchan10-Jun-20 0:37
mveRichard MacCutchan10-Jun-20 0:37 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 0:58
and180y10-Jun-20 0:58 
GeneralRe: Javascript function error. Pin
Richard MacCutchan10-Jun-20 1:18
mveRichard MacCutchan10-Jun-20 1:18 
AnswerRe: Javascript function error. Pin
Richard Deeming9-Jun-20 23:13
mveRichard Deeming9-Jun-20 23:13 
GeneralRe: Javascript function error. Pin
and180y9-Jun-20 23:25
and180y9-Jun-20 23:25 
GeneralRe: Javascript function error. Pin
Richard Deeming10-Jun-20 1:26
mveRichard Deeming10-Jun-20 1: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 1:55
and180y10-Jun-20 1:55 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 23:06
and180y10-Jun-20 23:06 
GeneralRe: Javascript function error. Pin
Richard Deeming10-Jun-20 23:30
mveRichard Deeming10-Jun-20 23:30 
GeneralRe: Javascript function error. Pin
and180y10-Jun-20 23:45
and180y10-Jun-20 23:45 

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.