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
user4323627
2 Answers 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
Barmar
789k57 gold badges555 silver badges669 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Barmar is correct. setting the milliseconds will solve your problem.
Comments
default
console.logto show the results for both.getTime()functions?ifis for comparing things, not looping.console.log(myDate.getTime(), date1.getTime())to see exactly what is being compared. It might not be what you expect.