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:

http://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);
        }

Open in new window


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;

Open in new window

forstersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>>Line  4:  //var diff = difference;
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
0
forstersAuthor Commented:
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)));
0
hieloCommented:
OK, but doesn't diff contain something like "hh:nn", where hh is the hours and nn are the minutes? If so, then:
Line 10: diff = Math.abs(diff);

is setting diff to NaN (not a number).  Math.abs expects a numeric value.  So, move:
var duration = diff;

to the top -- make it the first line within that "if" clause (line 2 on your post)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
forstersAuthor Commented:
ahhh thank you - that's resolved it, perfect many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.