user1002888
user1002888

Reputation: 1

How to do total 24 hrs validation maintaning 12 hrs format using javascript?

In my web app Form i ve two fields called startTime,closeTime which are repeated in every row of the table.now i ve to validate for 24hrs(1440 mins) from startTime of the first row to closeTime of the last row.but user enters time in 12 hr format means if startTime of the first row is 08:00 am then i ve to check upto 08:00 am of next day(every thing i am converting into minutes).i tried in several ways but unable to caliculations missing in am to pm/pm to am please help me.following is my code (onclick of first column of every row it ll be called)

var timeArray=[""],timeArray1=[""],timeNoon,closeNoon,temp=0,sumTime=0; 
function checkTime(){
var startTime,closeTime;
if(rowIndex!=0){//rowIndex is current row index
                    startTime=document.getElementById("logSheetDataTable").rows[rowIndex-1].cells[j].childNodes[0].tBodies[0].rows[0].cells[1].childNodes[0].value;
                    closeTime = document.getElementById("logSheetDataTable").rows[rowIndex-1].cells[j].childNodes[0].tBodies[0].rows[1].cells[1].childNodes[0].value;

                    timeNoon=startTime.substring(6);//to get am or pm
                    startTime = startTime.substring(0,5);//to get tome 08:00
                    timeArray = startTime.split(":");

                    closeNoon=closeTime.substring(6);//to get am or pm
                    closeTime1=closeTime.substring(0,5);
                    timeArray1=closeTime1.split(":");

                    if(timeNoon.toLowerCase()=="pm"){
                        startMin=parseInt((timeArray[0]*60))+parseInt(timeArray[1])+720;
                    }else if(timeNoon.toLowerCase()=="am"){
                        startMin=parseInt((timeArray[0]*60))+parseInt(timeArray[1]);
                    }

                    if(closeNoon.toLowerCase()=="pm"){
                        endMin=parseInt((timeArray1[0]*60))+parseInt(timeArray1[1]+720);
                    }else if(closeNoon.toLowerCase()=="am"){
                        endMin=parseInt((timeArray1[0]*60))+parseInt(timeArray1[1]);
                    }


                    if(startMin<endMin){
                        temp=endMin-startMin;
                    }else if(startMin>endMin){
                        temp=(1440-startMin)+endMin;
                    }

                    sumTime=sumTime+temp;
                    alert("sum: "+sumTime);
}

for sumTime i ve to check for 1440 mins.

Upvotes: 0

Views: 167

Answers (1)

forsvarir
forsvarir

Reputation: 10849

It sounds like you've got a table that looks something like this:

START      END
08:00 am    10:00 am
10:00 am    03:00 pm
03:00 pm    08:00 am

And you're saying that you want to validate that 24 hours has passed from the START in the first row and the END in the last row. If that's all you want to do what's all the rest of your code for?

The calculation you've got in your code looks like you're adding up the time between each START and END on the row, then summing these, presumably to check if you've got to 24 hours. You're not checking the first START + last END, so is this valid (three lots of 8 hours==24)?

START      END
01:00 am    09:00 am
01:00 am    09:00 am
01:00 am    09:00 am

When you're splitting out the string, you're always taking the first 5 characters and assuming that they are of the form HH:MM. Is this always correct (you're never going to encounter a time where for example the 0 prefix is missing, so you have 8:00am? Are you sure that there will always be exactly 1 space after the time, before the am/pm? Are you sure there is nothing after the am/pm? All of these will effect whether or not your string parsing works correctly...

Some of the ways that you're extracting numbers look a bit dodgy to me. For example:

startMin=parseInt((timeArray[0]*60))+parseInt(timeArray[1])+720;

Look carefully at the brackets. You calling parseInt on the result of (timeArray[0]*60). Presumably this should be calling it, before you try to do mathematical operations:

startMin=(parseInt(timeArray[0])*60)+parseInt(timeArray[1])+720;

This is a common problem with a lot of your calls to parseInt which may be causing some unexpected issues...

Now, looking at the calculation for the time difference between each start and end on a single row...

If the time is pm, add twelve hours (720 minutes) to convert it to 24 hours clock. You seem to be trying to do this, you're also converting it into minutes at the same time.

Now, you have three scenarios.

  1. START == END - This will either be 0 or 24 hours, currently you don't seem to cater for this.
  2. START > END - The day has rolled over between the START time and END time. So, you need to calculate the (time to the end of the day) + END time. So in minutes, you'd have 1440 - startMin + endMin. You seem to be doing this correctly...
  3. END > START - End is later than start, so you just need the difference: endMin - startMin, again you seem to be doing this correctly.

So, there must be something wrong with your code. I've had a bit of a fiddle and it turns our your problem is basically your bracketing. Because you've got them in the wrong place, your numbers are being used as strings sometimes + numbers others, so sometimes instead of addition, you get concatenation of the values which throws you way off...

The relevant fixes are:

if(timeNoon.toLowerCase()=="pm"){
    startMin=(parseInt(timeArray[0])*60)+parseInt(timeArray[1])+720;
}else if(timeNoon.toLowerCase()=="am"){
    startMin=(parseInt(timeArray[0])*60)+parseInt(timeArray[1]);
}                       

if(closeNoon.toLowerCase()=="pm"){                
    endMin=(parseInt(timeArray1[0])*60)+parseInt(timeArray1[1])+720;
}else if(closeNoon.toLowerCase()=="am"){
    endMin=(parseInt(timeArray1[0])*60)+parseInt(timeArray1[1]);
}

Upvotes: 1

Related Questions