Shanthi Shanthi - 27 days ago 8
jQuery Question

Need to find all the hours between a person wake up time and sleep time in jquery

I have a wakeup_time and a sleep_time

Eg: wakeup_time=09:00 pm and sleep_time= 11:00 am

I need to find all the hours(12 H format) between this 2 time.

The wakeup and sleep time will varry.

Expected result : 9 pm, 10pm, 12pm, 1 am, ......,10 am , 11 am

Answer

Another one . try this.

var id=0;
        var unit=wakeup_unit;
        var time;
        for(var i=0;i<=totalHours;i++){
        id=wakeup_time+i;

        if(id>=12){
            if(id==12){
                    time=id;
                    if(wakeup_time==12){

                    }else{
                        if(unit=="am"){
                            unit="pm";
                        }else if(unit=="pm"){
                            unit="am";
                        }else{
                            unit=unit;
                        }
                    }

                }else{
                    time=id-12;
                    if(time>=12){
                        if(time==12){
                            if(wakeup_time==12){

                    }else{
                        if(unit=="am"){
                        unit="pm";
                    }else if(unit=="pm"){
                        unit="am";
                    }else{
                        unit=unit;
                    }
                    }
                        }else{
                            time=time-12;
                        }
                    }

            }

        }else{
            time=id;
        }
        console.log(time +" "+ unit);


      if ((wakeup_unit == "am") && (sleep_unit == "pm")) {
                        if (wakeup_time == 12 && sleep_time != 12) {
                            var totalHours = (sleep_time + 12);
                        } else if(wakeup_time != 12 && sleep_time == 12){
                        var totalHours = (sleep_time -wakeup_time);
                        }else if (wakeup_time == 12 && sleep_time == 12) {
                            var totalHours = 12;
                        } else {
                            var totalHours = (sleep_time > wakeup_time) ? ((sleep_time + 12) - wakeup_time) : ((sleep_time + 12) - wakeup_time);
                        }
                    } else if ((wakeup_unit == "pm") && (sleep_unit == "am")) {
                        if (wakeup_time == 12 && sleep_time != 12) {
                            var totalHours = sleep_time + 12;
                        } else if (wakeup_time != 12 && sleep_time == 12) {
                            var totalHours = sleep_time - wakeup_time;
                        } else if (wakeup_time == 12 && sleep_time == 12) {
                            var totalHours = 12;
                        } else {
                            var totalHours = (sleep_time > wakeup_time) ? 24 - ((wakeup_time + 12) - sleep_time) : 24 - ((wakeup_time + 12) - sleep_time);
                        }
                    } else if ((wakeup_unit == "am") && (sleep_unit == "am")) {
                        if (wakeup_time == 12 && sleep_time == 12) {
                            var totalHours = 0;
                        }else if (wakeup_time == 12 && sleep_time != 12) {
                            var totalHours = sleep_time;
                        }else if (wakeup_time != 12 && sleep_time == 12) {
                            var totalHours = sleep_time+wakeup_time;
                        }else {
                            var totalHours = (sleep_time > wakeup_time) ? (sleep_time - wakeup_time) : 24 - (wakeup_time - sleep_time);
                        }

                    } else if ((wakeup_unit == "pm") && (sleep_unit == "pm")) {
                        if (wakeup_time == 12 && sleep_time == 12) {
                            var totalHours = 0;
                        }else if (wakeup_time == 12 && sleep_time != 12) {
                            var totalHours = sleep_time;
                        }else if (wakeup_time != 12 && sleep_time == 12) {
                            var totalHours = sleep_time+wakeup_time;
                        } else {
                            var totalHours = (sleep_time > wakeup_time) ? (sleep_time - wakeup_time) : 24 - (wakeup_time - sleep_time);
                        }

                    } else {
                        console.log("This should not come at all");
                    }