I am trying to compare two dates in javascript without the time portions. The first date comes from a jquery datepicker and the second date comes from a string.
Although I could swear that my method worked a while ago it looks like it is not working now.
My browser is Firefox but I also need my code to work in IE.
function selectedDateRetentionDaysElapsed() { var dateSelectedDate = $.datepicker.parseDate('dd/mm/yy', $('#selectedDate').val()); // dateSelectedDate is equal to date 2015-09-30T14:00:00.000Z var parsedRefDate = isoStringToDate('2015-11-10T00:00:00'); var reportingDate = getPredfinedDateWithoutTime(parsedRefDate); // reportingDate is equal to date 2015-11-10T13:00:00.000Z var businessDayCountFromCurrentReportingDate = getBusinessDayCount(dateSelectedDate,reportingDate); // businessDayCountFromCurrentReportingDate is equal to 39.9583333333336 if (businessDayCountFromCurrentReportingDate >= 40) { return true; } return false; } function isoStringToDate(dateStr) { var str = dateStr.split("T"); var date = str[0].split("-"); var time = str[1].split(":"); //constructor is new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]); return new Date(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0); } function getPredfinedDateWithoutTime(myDate) { myDate.setHours(0,0,0,0) return myDate; } My issues are...
- My isoStringToDate function is returning a date with a time even though I am not specifying a time.
- The setHours call on a date does not seem to be working either.
Can someone please help me with this.
timearray and then pass it to the constructor.isoStringToDate('2015-11-10T00:00:00')will return a Date object for the equivalent local time of 00:00:00 on that date (no need to further zero the hours), but the actual time value will be UTC, so its hours won't be zero unless the host system is set to UTC. So if dateSelectedDate really is 00:00:00UTC, then there will always be a time difference unless your system is also set to UTC. You need both to be 00:00:00 in the same timezone, preferably UTC so daylight saving boundaries are avoided too.