I am trying to fill an array/list with values from a time subtraction. I have start time in this format “09.00 AM” and End time in the same format ex: “06.00 PM”, both times are coming from two drop downs (values from 9 am to 9 pm with hourly increments) which are actually jquery time picker controls. This is what I am trying to do.. after user selects both times, I want to populate an array/list with individual “hour” mark values that fall between the two selected time values. For ex: selecting 9 am and 12 pm should give me values { “10.00 AM”, “11.00 AM”}.. selecting 10 AM to 4 PM should give me {“11.00 AM”, “12.00 PM”, “01.00 PM”, “02.00 PM”, “03.00 PM”}..
var FirstTime = container.find('#1time').val();
var SecondTime = container.find('#2time').val();
var busyTimes = [];
busyTimes = getBusyTimes(FirstTime , SecondTime);
Function js:
function getBusyTimes(first, second) {
var f = first.split(' '), s = second.split(' ');
if (first == '12.00 AM') f[0] = '0';
if (first == '12.00 PM') f[1] = 'AM';
if (second == '12.00 AM') s[0] = '24';
if (second == '12.00 PM') s[1] = 'AM';
f[0] = parseInt(f[0], 10) + (f[1] == 'PM' ? 12 : 0);
s[0] = parseInt(s[0], 10) + (s[1] == 'PM' ? 12 : 0);
int i = s[0] - f[0];
// I have difference in hours in "i"
//From here, I want to make use of value 'i' -- calculate in between
// hourly values and return the array back to
// original function above.
}
I have not tested this but it should work for what you are trying to do and it should operate faster than the calculations you are trying to do.