Skip to content

Instantly share code, notes, and snippets.

@andrewjcurrie
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewjcurrie/0f4f8598d7816c39e1fb to your computer and use it in GitHub Desktop.
Save andrewjcurrie/0f4f8598d7816c39e1fb to your computer and use it in GitHub Desktop.
Total Hours from 2 EE Time Select Fields
<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