1

The code below mentioned is for comparision of date. Both date1 and mydate have similar values,But if i compare its not entering if loop. Any help appreciated

 var date_arr = new Array( "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var Avl_date = document.getElementById("Available_Date").value;
var V_date1 = Avl_date.split('-');
var date1 = new Date (V_date1[2], date_arr.indexOf(V_date1[1]),V_date1[0]);
var myDate = new Date(); 
myDate.setHours(0,0,0);
 //Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> date1
 //Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> mydate
 if(myDate.getTime() === date1.getTime())
{
 //Not entering the loop
}
RichardBernards
3,0971 gold badge25 silver badges30 bronze badges
asked Dec 4, 2014 at 11:49
6
  • Have you used console.log to show the results for both .getTime() functions? Commented Dec 4, 2014 at 11:52
  • What is an if loop? if is for comparing things, not looping. Commented Dec 4, 2014 at 11:53
  • functionalities are not added in the code.If its true then,some thing should be displayed Commented Dec 4, 2014 at 11:55
  • 1
    You set the hours, minutes, and seconds, but not milliseconds. Commented Dec 4, 2014 at 11:56
  • You might want to put a console.log(myDate.getTime(), date1.getTime()) to see exactly what is being compared. It might not be what you expect. Commented Dec 4, 2014 at 11:56

2 Answers 2

2

You're not setting the milliseconds of myDate to 0, so it keeps its original milliseconds. Use:

myDate.setHours(0,0,0,0);
answered Dec 4, 2014 at 11:57
Sign up to request clarification or add additional context in comments.

Comments

0

Barmar is correct. setting the milliseconds will solve your problem.

answered Dec 4, 2014 at 11:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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