如何只使用javascript获取工作日。
以下是我目前的js;
<script>
$( document ).ready(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var d = new Date();
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
var currDay = d.getDate();
var day = days[ d.getDay() ];
var month = months[ d.getMonth() ];
var today = day + " " + ordinal_suffix_of(currDay) + " " + month;
var message2 = "The earliest delivery time today (" + today + ") is at 12:00noon";
var container = document.getElementById('availability-container');
var div = document.createElement('div');
div.className = 'availability';
div.innerHTML = "<p>" + message2; + "</p>";
container.appendChild(div);
$('.availability').addClass('animated pulse');
});
</script>
我怎样才能获得&#34;今天&#34;仅显示工作日而忽略周末,例如,如果是星期五,不要在星期一显示周六和周日。
PS:我能用PHP实现这一目标。
谢谢!
答案 0 :(得分:1)
<script>
$( document ).ready(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var d = new Date();
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
//----Edited Section
var currDay = d.getDate();
var day= d.getDay();
var numberOfDaysToAdd = 0;
if(day==6){
numberOfDaysToAdd= 2;
}else if(days==0){
numberOfDaysToAdd= 1;
}
currDay.setDate(currDay.getDate() + numberOfDaysToAdd);
var day = days[ currDay.getDay() ];
var month = months[ currDay.getMonth() ];
//--- End of Edited Section
var today = day + " " + ordinal_suffix_of(currDay) + " " + month;
var message2 = "The earliest delivery time today (" + today + ") is at 12:00noon";
var container = document.getElementById('availability-container');
var div = document.createElement('div');
div.className = 'availability';
div.innerHTML = "<p>" + message2; + "</p>";
container.appendChild(div);
$('.availability').addClass('animated pulse');
});
</script>
答案 1 :(得分:0)
下面的函数将始终确保var someDate是工作日 编辑:第二次循环错误。
var someDate = new Date();
var day= d.getDay();
var numberOfDaysToAdd = 0;
if(day==6){
numberOfDaysToAdd= 2;
}else if(days==0){
numberOfDaysToAdd= 1;
}
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
答案 2 :(得分:0)
试试这个:
while
循环直到满足条件会有所帮助!
$(document).ready(function() {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var daysToAvoid = ['Friday', 'Saturday', 'Sunday'];
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
var d = new Date();
var currDay = d.getDate();
var day = days[d.getDay()];
var month = months[d.getMonth()];
while (daysToAvoid.indexOf(day) !== -1) {
d.setDate(currDay + 1);
currDay = d.getDate();
day = days[d.getDay()];
month = months[d.getMonth()];
}
var today = day + " " + ordinal_suffix_of(currDay) + " " + month;
var message2 = "The earliest delivery time today (" + today + ") is at 12:00noon";
var container = document.getElementById('availability-container');
var div = document.createElement('div');
div.className = 'availability';
div.innerHTML = "<p>" + message2 + "</p>";
container.appendChild(div);
$('.availability').addClass('animated pulse');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="availability-container"></div>
答案 3 :(得分:0)
你可以使用这样的东西。
function getNextWeekday()
{
var date = new Date();
date.setDate(date.getDate() + 1);
var day = date.getDay();
if (day == 0)
{
date.setDate(date.getDate() + 1);
}
else if (day == 6)
{
date.setDate(date.getDate() + 2);
}
return date;
}