Reputation: 1
I am displaying the following code for next 5 days
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);
}
I want to exclude Sundays from this list
Upvotes: 0
Views: 91
Reputation: 21575
As T.J Crowder said, you can use Date.getDay()
to get the current weekday in the week. Giving some integer from 0
to 6
where 0
is Sunday and 6
is Saturday.
To show the next weekdays I supposed we don't want to print a bunch of numbers on the screen, so we can use a weekdays
array to go from numbers to their corresponding text:
var weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
Now we can just use a for
loop to scroll through these, starting from the todays day you get from using .getDay()
. Note that if you go over we want to go back to 0
, so I'll use a separate variable j
in the loop for that:
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]);
}
To show it can overflow and move back to Sunday, Here is a fiddle that prints the next 20 days.
To exclude Sunday, simply check that if(j != 0)
, then print:
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
}
Although since today is Sunday, suppose you want to include Today's Sunday, but not the next Sundays. Simply change the if statement to if( j != 0 || i == 0 )
, which will make an exception for the first element. Here is an example of that.
Upvotes: 0
Reputation: 962
You can use the getDay method to get the day of the week:
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);
}
Upvotes: 0
Reputation: 1074839
You can tell what day of the week a Date
instance represents using its getDay
function:
if (dt.getDay() === 0) {
// It's Sunday
}
else {
// It isn't
}
I figure you can take it from there... :-)
Upvotes: 1