0

Ok I cant get my head round this, ive looked at so many posts on SF but cant figure it out.
I need to campare server time with hardcorded time.

i have try this

            var breackfastCutOffTime    = "5:30";
            var lunchCutOffTime         = "10:00";
            var dinnerCutOffTime        = "17:00";


            var serverTime = currentDate.getHours()+ ":" + currentDate.getMinutes();              

            if(Date.parse(toDay) ==  Date.parse(selectedDate)){
                if(serverTime >= breackfastCutOffTime ){
                    document.getElementById("breakfast").disabled = true;
                }else if(serverTime >= lunchCutOffTime){
                    document.getElementById("lunch").disabled = true;
                }else if(serverTime >= dinnerCutOffTime){
                    document.getElementById("dinner").disabled = true;
                }           
            } 

I know this cant compare because time are in text format.Some one pleace help me to compleate this.

1
  • make your cutofftimes date string instead like var breackfastCutOffTime = new Date(); breackfastCutOffTime.setHours(parseInt('05',10),parseInt('30',10),parseInt('00',10),0); then comparison will work. Commented Dec 11, 2015 at 8:09

3 Answers 3

2

You can write a function which converts Time(hh:mm) in minutes(mm) only. Then you can compare two times and do your operations accordingly.

pseudocode:

function convert(string time) int {
  //split the time by ':'
  //convert the string hh,mm to int hour,mm
  //calculate total minutes 
  //return total  minutes
}
Sign up to request clarification or add additional context in comments.

Comments

1

Refer Date Api for more info.

Below is your modified code. I have commented few lines. Modify according to your logic

Fiddler link

//var breackfastCutOffTime    = "5:30";
var breackfastCutOffTime    = new Date();
breackfastCutOffTime.setHours(5);
breackfastCutOffTime.setMinutes(30);

//var lunchCutOffTime         = "10:00";
var lunchCutOffTime = new Date();
lunchCutOffTime.setHours(10);
lunchCutOffTime.setMinutes(00);

//var dinnerCutOffTime = "17:00";
var dinnerCutOffTime = new Date();
dinnerCutOffTime.setHours(17);
dinnerCutOffTime.setMinutes(00)

var serverTime = new Date();

//var serverTime = currentDate.getHours()+ ":" + currentDate.getMinutes();              

//if(Date.parse(toDay) ==  Date.parse(selectedDate)){
  if(serverTime >= breackfastCutOffTime ){
    console.log("breackfastCutOffTime");
    //document.getElementById("breakfast").disabled = true;
  }else if(serverTime >= lunchCutOffTime){
    //document.getElementById("lunch").disabled = true;
    console.log("lunchCutOffTime");
  }else if(serverTime >= dinnerCutOffTime){
    //document.getElementById("dinner").disabled = true;
    console.log("dinnerCutOffTime");
  }           
//} 

Explanation:

Instead of converting the Date format to string i am creating new instance of the date for each cutoff time and setting desired time to that object.

Finally compare the date objects directly.

Note: The Date objects are set according to your timezone.

Comments

0

You can pass the breackfastCutOffTime,lunchCutOffTime and dinnerCutOffTime to the below function and it will return true if they are equal to or greater than current time.

function compareTime(timeToCompare) {
            var currentDate = new Date();
            var tempDate = new Date();
            var timeToCompareArray = timeToCompare.split(":");
            tempDate.setHours(timeToCompareArray[0]);
            tempDate.setMinutes(timeToCompareArray[1]);
            if (tempDate >= currentDate) {
                return true;
            } else {
                return false;
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.