1

I wanted to compare two dates which are in below format:

9/10/2014 5:22 PM

I have two such dates and i want to compare them.

For that i have written below code:

 var StartDateTime = ($("#dtpStartDate").data("kendoDateTimePicker")).value().toLocaleString("en-US");

 var currentDateTime = new Date();

 if (StartDateTime < currentDateTime.toLocaleString()) {
            alert("StartDateTime is less");
            flagValidation = false;
        }
        else {
            alert("StartDateTime is large");
        }

But actually when StartDateTime is less than currentdatetime then also Its showing alert "StartDateTime is large".

Please help me.

How can i compare the dates correctly?

1
  • 4
    You're not comparing dates here, you're comparing strings. Use if($("#dtpStartDate").data("kendoDateTimePicker")).value() < currentDateTime) instead Commented Sep 10, 2014 at 12:03

1 Answer 1

2

You have convert object to date format to compare and consider all components of date. for your reference (Check the hard coded minutes..)

if(new Date("9/10/2014 5:23 PM") <= new Date("9/10/2014 5:22 PM")){
  alert("StartDateTime is less");
}
else{
  alert("StartDateTime is large");
}
Sign up to request clarification or add additional context in comments.

1 Comment

dhanyawad patil bhau :)

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.