forsters
asked on
Error: Object doesn't support property or method 'split'
Hi Experts,
This is Question 3 of hopefully 3!
My previous questions were to do with combining and formatting a date with hours and minutes to make a comparison between two datetimes:
https://www.experts-exchange.com/questions/28699609/Formatting-date-and-time-JavaScript.html
I now need to split that result back into hours and minutes for a couple of final calculations:
I need to round down the results to the nearest quarter of an hour
and
I need to display the result as a rounded decimal time i.e. two and a quarter hours would be 2.25
so my final code will display three results:
actual time difference (hh:mm)
rounded time difference (hh:mm)
rounded time difference as a decimal (hh.mm)
My code so far gets the following error:
Error: Object doesn't support property or method 'split'
This part of the code is tested and working many thanks to assistance from experts:
This is the part I get the error on - I presume this line is not working - var duration = diff; as the related Text Box I am using to display the result doesn't get populated...? Sorry bit out of my depth
This is Question 3 of hopefully 3!
My previous questions were to do with combining and formatting a date with hours and minutes to make a comparison between two datetimes:
https://www.experts-exchange.com/questions/28699609/Formatting-date-and-time-JavaScript.html
I now need to split that result back into hours and minutes for a couple of final calculations:
I need to round down the results to the nearest quarter of an hour
and
I need to display the result as a rounded decimal time i.e. two and a quarter hours would be 2.25
so my final code will display three results:
actual time difference (hh:mm)
rounded time difference (hh:mm)
rounded time difference as a decimal (hh.mm)
My code so far gets the following error:
Error: Object doesn't support property or method 'split'
This part of the code is tested and working many thanks to assistance from experts:
function TimeDuration() {
var therate = document.getElementById('<%=DDLRate.ClientID%>').value;
var startdate = document.getElementById('<%=TxtStartDate.ClientID%>').value;
var enddate = document.getElementById('<%=TxtEndDate.ClientID%>').value;
var starthours = document.getElementById('<%=DDLStartHrs.ClientID%>').value;
var startmins = document.getElementById('<%=DDLStartMins.ClientID%>').value;
var endhours = document.getElementById('<%=DDLStopHrs.ClientID%>').value;
var endmins = document.getElementById('<%=DDLStopMins.ClientID%>').value;
var startparts = startdate.split('/');
var endparts = enddate.split('/');
var mystartdate = startparts[2] + "-" + startparts[1] + "-" + startparts[0];
var myenddate = endparts[2] + "-" + endparts[1] + "-" + endparts[0];
var started = (mystartdate + 'T' + starthours + ':' + startmins + ':00.000Z')
var ended = (myenddate + 'T' + endhours + ':' + endmins + ':00.000Z');
var diff = daysBetween(new Date(Date.parse(started)), new Date(Date.parse(ended)));
if (startdate != '') {
document.getElementById('<%=Txttest1.ClientID%>').value = started;
}
if (enddate != '') {
document.getElementById('<%=Txttest2.ClientID%>').value = ended;
}
if (startdate != '' && enddate != '') {
document.getElementById('<%=Txttest3.ClientID%>').value = diff;
}
function daysBetween(date1, date2) {
//Get 1 day in milliseconds
var one_day = 1000 * 60 * 60 * 24;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
//take out milliseconds
difference_ms = difference_ms / 1000;
var seconds = Math.floor(difference_ms % 60);
difference_ms = difference_ms / 60;
var minutes = Math.floor(difference_ms % 60);
difference_ms = difference_ms / 60;
var hours = Math.floor(difference_ms % 24);
var days = Math.floor(difference_ms / 24);
//return (hours < 10 ? '0'+hours:hours) + ':' + (minutes<10 ? '0'+minutes:minutes) + ':' + (seconds<10?'0'+seconds:seconds);
return (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes);
}
This is the part I get the error on - I presume this line is not working - var duration = diff; as the related Text Box I am using to display the result doesn't get populated...? Sorry bit out of my depth
if (startdate != '' && starthours != '' && startmins != '' && endhours != '' && enddate != '' && endmins != '') {
// WORK OUT DIFF IN MIN
//var diff = difference;
// SET THE SIGN IF START > END
var sign = (diff < 0) ? '' : '';
// WORK WITH ABS VALUE
diff = Math.abs(diff);
// SET THE DIFF IN MINUTES
// CREATE A FORMATTED STRING hh:mm
var hours = parseInt(diff / 60);
var min = diff - (hours * 60);
// LETS PAD THE STRING
min = "00" + min;
hours = "00" + hours;
var hoursdecimal = "";
var hoursrounded = "";
var duration = diff;
//var duration = sign + hours.substr(hours.length - 2) + ':' + min.substring(min.length - 2);
if (duration.split(':')[1] >= '00' && duration.split(':')[1] <= '14') {
hoursdecimal = duration.split(':')[0] + ".00";
hoursrounded = duration.split(':')[0] + ":00";
}
if (duration.split(':')[1] >= '15' && duration.split(':')[1] <= '29') {
hoursdecimal = duration.split(':')[0] + ".25";
hoursrounded = duration.split(':')[0] + ":15";
}
if (duration.split(':')[1] >= '30' && duration.split(':')[1] <= '44') {
hoursdecimal = duration.split(':')[0] + ".50";
hoursrounded = duration.split(':')[0] + ":30";
}
if (duration.split(':')[1] >= '45' && duration.split(':')[1] <= '59') {
hoursdecimal = duration.split(':')[0] + ".75";
hoursrounded = duration.split(':')[0] + ":45";
}
// SET THE FORMATTED DIFF
document.getElementById('<%=TxtDuration.ClientID%>').value = duration;
// SET THE FORMATTED DIFF
document.getElementById('<%=TxtDurationRounded.ClientID%>').value = hoursrounded;
// SET THE FORMATTED DIFF
document.getElementById('<%=TxtDurationDecimal.ClientID%>').value = hoursdecimal;
ASKER
ah apologies - the code is part of the original function so diff is already defined:
var diff = daysBetween(new Date(Date.parse(started)), new Date(Date.parse(ended)));
var diff = daysBetween(new Date(Date.parse(started)),
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ahhh thank you - that's resolved it, perfect many thanks
You have commented out diff, so if it is undefined, then on:
>>Line 10: diff = Math.abs(diff);
diff would remain undefined. You need to initialize diff