【发布时间】:2015-04-09 17:38:56
【问题描述】:
我对像 javascript 这样的浏览器端脚本非常薄弱,因为我是一名 php 开发人员。我需要得到周六和周日的日期。我找到了很多计算计数的答案,但还没有找到在周六和周日获取日期的方法。我尝试了这些:
Date.prototype.endOfWeek = function(){
return new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() + 6 - this.getDay()
);
};
var now = new Date();
// returns next saturday; and returns saturday if it is saturday today.
alert(now.endOfWeek() );
它只在下一个星期六返回给我。请帮我得到这个
我也试过这个,但它返回给我计数
function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1) return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
if (iWeekday1 <= iWeekday2) {
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
alert(iDateDiff);
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
return (iDateDiff + 1); // add 1 because dates are inclusive
}
alert(calcBusinessDays(new Date("August 11, 2010 11:13:00"),new Date("August 16, 2010 11:13:00")));
【问题讨论】:
-
第一个函数返回下一个星期六的日期对象。是否要返回包含星期六和星期日的两个 Date 对象的数组?
-
您想返回两个日期之间的所有个星期六和星期天,就在下一个周末,还是什么?
标签: javascript date datetime