我将在接下来的5天内显示以下代码
function setDateTime() {
var timesOffice = (officeTimes[officeID] + "").split(",");
//alert(officeTimes[officeID]+":12:"+timesOffice[0]);
var dt = new Date(correctDate);
var dateOptions = "";
var firstdateString = "";
var totalDays = 5;
for (i = 0; i < totalDays; i++) {
var sateString = dt.getFullYear() + " " + monthNames[dt.getMonth()] + " " + (dt.getDate());
//console.log("i:"+i+"s:"+sateString);
dateFlag = 0;
var j = 0;
for (j = 0; j < timesOffice.length; j++) {
if (checkValidDateTime(sateString, timesOffice[j])) {
dateFlag = 1;
break;
}
}
dt.setDate(dt.getDate() + 1);
if (dateFlag == 0) {
totalDays++;
continue;
}
if (firstdateString == "") firstdateString = sateString;
dateOptions = dateOptions + '<option value="' + sateString + '">' + sateString + '</option>';
}
$(".date").html(dateOptions);
}
我想从此列表中排除星期日
答案 0 :(得分:1)
您可以使用getDay
function告诉Date
个实例代表哪一天:
if (dt.getDay() === 0) {
// It's Sunday
}
else {
// It isn't
}
我想你可以从那里拿走......: - )
答案 1 :(得分:0)
您可以使用getDay方法获取星期几:
function setDateTime() {
var timesOffice = (officeTimes[officeID] + "").split(",");
//alert(officeTimes[officeID]+":12:"+timesOffice[0]);
var dt = new Date(correctDate);
var dateOptions = "";
var firstdateString = "";
var totalDays = 5;
int i=0;
while(i<totalDays) {
if(dt.getDay() != 0) // exclude Sundays
{
var sateString = dt.getFullYear() + " " + monthNames[dt.getMonth()] + " " + (dt.getDate());
//console.log("i:"+i+"s:"+sateString);
dateFlag = 0;
var j = 0;
for (j = 0; j < timesOffice.length; j++) {
if (checkValidDateTime(sateString, timesOffice[j])) {
dateFlag = 1;
break;
}
}
if (firstdateString == "") firstdateString = sateString;
dateOptions = dateOptions + '<option value="' + sateString + '">' + sateString + '</option>';
i++;
}
dt.setDate(dt.getDate() + 1);
}
$(".date").html(dateOptions);
}
答案 2 :(得分:0)
正如T.J Crowder所说,您可以使用Date.getDay()
获取当周的当前工作日。从0
到6
给出一些整数,其中0
星期日和6
星期六。
为了显示下一个工作日我想我们不想在屏幕上打印一堆数字,所以我们可以使用weekdays
数组从数字到相应的文本:
var weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
现在我们可以使用for
循环滚动浏览这些循环,从使用.getDay()
的今天开始。请注意,如果您继续我们想要返回0
,那么我将在循环中使用单独的变量j
:
var weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var date = new Date();
var day = date.getDay(); // Current day
var numDays = 5; // Number next days
for(var i = day; i <= day + numDays; i++ ) {
var j = i;
j = j % weekdays.length;
console.log(weekdays[j]);
}
要显示它可以溢出并返回到星期日, Here is a fiddle 会打印接下来的20天。
要排除星期日,只需检查if(j != 0)
,然后打印:
for(var i = day; i <= day + numDays; i++ ) {
var j = i;
j = j % weekdays.length;
if( j != 0 )
console.log(weekdays[j]); // Only prints non-Sundays
}
虽然今天是星期天,但是假设你想要包括今天的星期日,而不是下一个星期日。只需将if语句更改为if( j != 0 || i == 0 )
,这将使第一个元素成为例外。 Here is an example 。