Last active
August 29, 2015 14:16
-
-
Save andrewjcurrie/0f4f8598d7816c39e1fb to your computer and use it in GitHub Desktop.
Total Hours from 2 EE Time Select Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
$(function() { | |
$('#arrival_hour, #arrival_minutes, #arrival_noon, #departure_hour, #departure_minutes, #departure_noon').on('change', function(event) { | |
var arrival_hour = $('#arrival_hour').val(); | |
var arrival_minute = $('#arrival_minutes').val(); | |
var departure_hour = $('#departure_hour').val(); | |
var departure_minute = $('#departure_minutes').val(); | |
if (arrival_hour && arrival_minute && departure_hour && departure_minute) { | |
var today = new Date(); | |
var arrival = new Date(today.getFullYear(), today.getMonth(), today.getDate(), arrival_hour, arrival_minute); | |
var departure = new Date(today.getFullYear(), today.getMonth(), today.getDate(), departure_hour, departure_minute); | |
console.log(arrival, departure); | |
var diff = new Date(departure - arrival); | |
var diffHours = Math.floor((diff % 86400000) / 3600000); | |
var diffMins = Math.floor(((diff % 86400000) % 3600000) / 60000); | |
var amount = diffHours + parseFloat(diffMins/60); | |
$('#time_spent').text(amount.toFixed(2)); | |
$('#hours_total').val(amount.toFixed(2)); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment