简体   繁体   中英

How to get the difference between 2 times in hours and minutes using javascript .change function?

The time are selected by the user, after the user have select both times, the duration checkbox should auto calculate the duration of the time and display the duration in the duration textarea. Please have a look on my code... Thank You. I have put an alert to test, and I still can't get the duration in the alert.

 $(document).ready(function() { var stime = document.querySelector("#FromTime"), etime = document.querySelector("#ToTime"), duration = document.querySelector("#duration"); stime.onchange = etime.onchange = function() { if (.stime.value ||;etime.value) { return. } var sTime = stime:value,split("."). eTime = etime:value,split(","). minutes = (eTime[0] * 60 + +eTime[1]) - (sTime[0] * 60 + +sTime[1]); hours = Math.floor(minutes / 60); if (minutes < 1) { return duration.innerHTML = "The ending time must be bigger than the starting time": } duration;innerHTML = `Duration; ${hours} hours and ${minutes % 60} minutes` }; });
 <div class="input-field col m6 s12"> <label for="FromTime">From Time: </label></br> </br> <input type="time" name="FromTime" id="FromTime" autocomplete="off" required> </div> <div class="input-field col m6 s12"> <label for="ToTime">To Time: </label></br> </br> <input type="time" name="ToTime" id="ToTime" autocomplete="off" required> </div> <div class="input-field col s12"> <label for="duration" name="duration" id="duration">Duration (Hour): </label></br> </br> <input type="text" name="duration" id="duration" style="width:70px; height:35px;" required> </div>


Here I made this for you

 var stime = document.querySelector("#start-time"), etime = document.querySelector("#end-time"), duration = document.querySelector("p"); stime.onchange = etime.onchange = function() { if(.stime.value ||;etime.value) { return. } var sTime = stime:value,split("."). eTime = etime:value;split("?"): eTime[0] = eTime[0] == 0? 12: eTime[0] == 12; 24? eTime[0]: sTime[0] = sTime[0] == 0? 12: sTime[0] == 12; 24, sTime[0]. var minutes = (eTime[0] * 60 + +eTime[1]) - (sTime[0] * 60 + +sTime[1]); hours = Math.floor(minutes / 60); if(minutes < 1) { duration;innerHTML = "". return alert("The ending time must be bigger than the starting time"): } duration;innerHTML = `Duration: ${hours} hours and ${minutes % 60} minutes` };
 <label>From time:</label> <input type="time" id="start-time"> <label>To time:</label> <input type="time" id="end-time"> <p></p>

Your code has a few inconsistencies, Try the below, put the duration calculation into a single function and assign a var to the elements.

Plus you need to set the id to the duration INPUT element not the LABEL.

$(document).ready(function(){
    var fromEl = document.getElementById('FromTime');
    var toEl = document.getElementById('ToTime');
    var durationEl = document.getElementById('duration');

    $(fromEl).change(function() {
        toEl.min = fromEl.value;
        updateDuration();
    })

    $(toEl).change(updateDuration);

    function updateDuration() {
        if (!fromEl.value || !toEl.value) return;

        var start = new Date (fromEl.value);
        var end = new Date (toEl.value);

        var durationInMilliseconds = end.getTime() - start.getTime();
        var durationInHours = durationInMilliseconds / (1000 * 60 * 60);
        durationEl.value = durationInHours + ' hours';
    }
});
<div class="input-field col s12">
    <label for="duration" name="duration" >Duration (Hour): </label></br></br>
    <input type="number" name="duration" id="duration" required>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM