admin管理员组文章数量:1332352
I am trying to pare two dates in javascript without the time portions. The first date es from a jquery datepicker and the second date es 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.
I am trying to pare two dates in javascript without the time portions. The first date es from a jquery datepicker and the second date es 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.
Share Improve this question edited Nov 11, 2015 at 6:38 Richie asked Nov 11, 2015 at 5:41 RichieRichie 5,19927 gold badges102 silver badges193 bronze badges 4-
1. You are specifying time. You parse a string, extract values to
time
array and then pass it to the constructor. – Yeldar Kurmangaliyev Commented Nov 11, 2015 at 5:44 - There is no way that '2015-11-10T00:00:00' converted to a Date object will be the equivalent of '2015-09-30T13:00:00.000Z', those dates are 41 days apart. Also, the isoStringToDate function zeros the local hours, so unless you are in the GMT timezone, the UTC (i.e. Z) time zone, the time ponent will not be 00:00:00. – RobG Commented Nov 11, 2015 at 5:53
- Hi Yelda. That's true but the time values are 00:00:00. So isn't that effectively a date without a time? – Richie Commented Nov 11, 2015 at 6:45
-
1
It's still not what you think.
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. – RobG Commented Nov 11, 2015 at 6:50
1 Answer
Reset to default 4The simplest way to get the number of whole days between two dates is to create two date objects for the subject dates that are set to the same time. Noon is convenient as it means the date part is unaffected by daylight saving (some places introduce it at midnight) if you happen to print out just the date part.
The following does all calculations in the time zone of the host system. UTC could be used (and the hours set to 0 as daylight saving isn't an issue at all), but it's more to type.
E.g.:
function differenceInDays(d0, d1) {
// Copy dates so don't affect originals
d0 = new Date(+d0);
d1 = new Date(+d1);
// Set to noon
d0.setHours(12,0,0,0);
d1.setHours(12,0,0,0);
// Get difference in whole days, divide by milliseconds in one day
// and round to remove any daylight saving boundary effects
return Math.round((d1-d0) / 8.64e7)
}
// Difference between 2015-11-12T17:35:32.124 and 2015-12-01T07:15:54.999
document.write(differenceInDays(new Date(2015,10,12,17,35,32,124),
new Date(2015,11,01,07,15,54,999)));
本文标签: JavascriptComparing dates without timeStack Overflow
版权声明:本文标题:Javascript - Comparing dates without time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322428a2453058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论