0

我目前一直在使用这个脚本

<script type="text/javascript">
var _next = new Date(new Date() * 1 + 24*60*60*1000*9);
document.write(" Your expected delivery date is " + (_next.getMonth() + 1) + "/" + _next.getDate() + "/" + _next.getFullYear());
</script>  

我想知道如果可能的话,我该如何排除周末。

4

4 回答 4

1

如果您愿意,只需添加时间。这是预计在下一个工作日交付的代码。

function myFunction() { 
var d = new Date();
var weekday = new Array(7);
weekday[0] = "sunday";
weekday[1] = "monday";
weekday[2] = "tuesday";
weekday[3] = "wednesday";
weekday[4] = "thursday";
weekday[5] = "friday";
weekday[6] = "saturday";
var monthday = new Array(12);
monthday[0] = "01";
monthday[1] = "02";
monthday[2] = "03";
monthday[3] = "04";
monthday[4] = "05";
monthday[5] = "06";
monthday[6] = "07";
monthday[7] = "08";
monthday[8] = "09";
monthday[9] = "10";
monthday[10] = "11";
monthday[11] = "12";


if (d.getDay() > 0 && d.getDay() <= 4) {
    d.setHours(d.getHours() + 24);
    var deliver = weekday[d.getDay()];
    var day = d.getDate();
    var month = monthday[d.getMonth()];
    var year = d .getFullYear();
    document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
} else if (d.getDay() == 0) {
    d.setHours(d.getHours() + 48);
    var deliver = weekday[d.getDay()];
    var day = d.getDate();
    var month = monthday[d.getMonth()];
    var year = d .getFullYear();
    document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
} else {
    d.setHours(d.getHours() + 72);
    var deliver = weekday[d.getDay()];
    var day = d.getDate();
    var month = monthday[d.getMonth()];
    var year = d .getFullYear();
    document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
}
}
myFunction()
于 2014-09-22T08:17:45.533 回答
0

尝试这个:

function deliver(inDays, startingOn){
  var s, f = 0, d;
  if(!inDays)inDays = 0;
  s = !startingOn ? new Date : new Date(startingOn);
  for(var i=0,n,t=0,l=inDays; i<l; i++,t+=86400000){
    n = new Date(s.getTime()+t).getDay();
    if(n === 0 || n === 6)f++;
  }
  d = new Date(s.getTime()+86400000*(inDays+f));
  return 'Your expected delivery date is '+d.toLocaleDateString();
}
// same day delivery
console.log(deliver());
// deliver in 9 days stating today
console.log(deliver(9));
// deliver in 9 days starting on October 12, 2013 - must be a valid Date String
console.log(deliver(9, 'October 12, 2013'));
/* Note that in the last example the Date starts on the weekend, therefore same
   day becomes Monday, which if you don't work weekends is the first day you
   would see the order anyways.
*/

这很适合在交付东西时向您的客户展示。确保根据您所在的位置,使用 PHP 等服务器端语言设置数据库的实际日期。

你应该为此付钱给我,因为它甚至在闰年也能起作用。哈!

于 2013-10-04T21:38:11.190 回答
0
<script type="text/javascript"> 
<!--
var myDate=new Date();
if ( myDate.getHours() < 12 ) // less than 12pm
  {
  var daystodeliver = [7,5,5,5,5,8,7][myDate.getDay()];
  }
else
  {
  var daystodeliver = [7,7,7,7,7,9,8][myDate.getDay()];
  }
myDate.setDate(myDate.getDate()+daystodeliver);
document.write(['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']     [myDate.getDay()]);
var dayofmonth =  myDate.getDate();
suffix = ((dayofmonth < 10)||(dayofmonth > 20)) ?   ['th','st','nd','rd','th','th','th','th','th','th'][dayofmonth % 10] : 'th';
document.write(' ' + dayofmonth + suffix + ' ');
 document.write(['January','February','March','April','May','June','July','August','Septembe    r','October','November','December'][myDate.getMonth()]);
// -->
于 2013-10-06T16:41:45.473 回答
0

试试这个来获取日期

我写了一个例程,您提供一个日期对象,应用方法,通过单独列表中的公共假期,并跳过包括周末在内的任何假期/公共假期。

它的这篇文章

// array of ISO YYYY-MM-DD format dates
publicHolidays = {
  uk:["2020-01-01","2020-04-10","2020-04-13","2020-05-08","2020-05-25",
    "2020-08-03","2020-08-31","2020-12-25","2020-12-28"],
   usa:["2020-01-01","2020-01-20","2020-02-14","2020-02-17","2020-04-10",
    "2020-04-12","2020-05-10","2020-05-25","2020-06-21","2020-07-03",
    "2020-07-04","2020-09-07","2020-10-12","2020-10-31","2020,11,11",
    "2020-11-26","2020-12-25"]
}
// check if there is a match in the array
Date.prototype.isPublicHoliday = function( data ){// we check for a public holiday
    if(!data) return 1;
return data.indexOf(this.toISOString().slice(0,10))>-1? 0:1;
}

// calculation of business days
Date.prototype.businessDays = function( d, holidays ){
    var holidays = holidays || false, t = new Date( this ); // copy date.  
    while( d ){ // we loop while d is not zero...       
    t.setDate( t.getDate() + 1 ); // set a date and test it
    switch( t.getDay() ){ // switch is used to allow easier addition of other days of the week
            case 0: case 6: break;// sunday & saturday
            default: // check if we are a public holiday or not
            d -= t.isPublicHoliday( holidays ); 
        } 

    }
    return t.toISOString().slice(0,10); // just the YYY-MM-DD 
}

// dummy var, could be a form field input
OrderDate = "2020-02-12";
// test with a UK holiday date
var deliveryDate = new Date(OrderDate).businessDays(7, publicHolidays.usa);
// expected output 2020-02-25
console.log("Order date: %s, Delivery date: %s",OrderDate,deliveryDate );
于 2020-02-04T21:59:27.407 回答