JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css">
<input type="text" id="txt1" value="13/06/2013" />
<br />
<select id="ddlHourTime">
<option>06</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
</select>
<br />
<select id="ddlMinuteTime">
<option>00</option>
<option>15</option>
<option>30</option>
<option>45</option>
</select>
<br />
<br />
<input type="button" id="btn1" value="test" />
JavaScript
$(document).ready(function () {
'use strict';
var calcWorkHours = function calcWorkHours(start, end) {
var total = (end - start) / (1000 * 60 * 60 * 24), // compute difference in days
totalDays = parseInt(total, 10), // trim fractional portion
remainder = (total - totalDays) * 24, // save fractional portion in hours
startingDay = start.getDay(), // starting weekday
endingDay = end.getDay(), // ending weekday
possibleWorkingDays = (totalDays * 5),
actualOccuringWeekends = (startingDay - endingDay) * 2,
workDays = (possibleWorkingDays - actualOccuringWeekends) / 7;
if (endingDay === 6) {
workDays -= 1; // ends on a weekend, subtract a day
}
if (startingDay === 0) {
workDays -= 1; // starts on a weekend, subtract a day
}
return (workDays * 24) + remainder; // convert to hours and add back fractional portion
},
parseInputs = function parseInputs(date, hour, minute) {
var dmy = date.split("/"),
now = new Date(),
thisMinute = now.getMonth() + 1 + "-" + now.getDate() + "-" + now.getFullYear() + " " + now.getHours() + ":" + now.getMinutes() + ":00",
start = Date.parse(dmy[1] + "-" + dmy[0] + "-" + dmy[2] + " " + hour + ":" + minute + ":00"),
end = Date.parse(thisMinute);
start = new Date(start);
end = new Date(end);
return calcWorkHours(start, end);
};
$("#btn1").click(function (event) {
var date = $("#txt1").val(),
hour = $("#ddlHourTime").val(),
minute = $("#ddlMinuteTime").val(),
elapsed = parseInputs(date, hour, minute);
window.alert(elapsed);
});
$('#txt1').datepicker({
...