开发者

date function to calculate only week days

开发者 https://www.devze.com 2023-02-07 10:04 出处:网络
requirement is onetext box which contain开发者_开发知识库s current date that date should show current date +next five days which should calculate only week days(monday to friday) and wants to display

requirement is one text box which contain开发者_开发知识库s current date that date should show current date + next five days which should calculate only week days(monday to friday) and wants to display in text box anybody send me this code plz let me know if any description


It's not clear exactly what you're looking to do, but these things should help:

  1. You can create Date objects for specific dates using the three-argument constructor function: Date(year, month, date) where year is the full year (e.g., 2011), month is the month number (0 = January), and date is the day of the month (1-28/29/30/31 depending). E.g.:

    var dt = new Date(2011, 0, 31); // January 31st, 2011
    
  2. You can tell what day of the week a given Date represents by using its getDay function; that returns a number with 0 = Sunday, 1 = Monday, etc.:

    var day = dt.getDay();
    if (day == 6/*Saturday*/ || day == 0/*Sunday*/) {
        // It's Saturday or Sunday
    }
    else {
        // It isn't, it's a weekday
    }
    
  3. You can advance a Date instance to the next day like so:

    dt = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + 1);
    

    The Date constructor will handle wrapping to the next month (and possibly year) for you.

  4. You can set the value of a text field by assigning to the value property of the DOM element for the field. You can get the DOM element any of several ways. One way is to give the element an id attribute (which must be unique) and then use

    var element = document.getElementById("idstring");
    

    ...to look it up.

  5. You'll obviously need a loop to build up your list of five weekdays.


This should solve part of your problem, at least on how to add x days to a Date and skip the weekends...

Date.prototype.addWeekDays = function(days)
    {
        var d = this;
        var dow = d.getUTCDay();

        var daysToAdd = (dow + days < 6) ? days : days + 2;

       return new Date(
        d.getUTCFullYear(), 
        d.getUTCMonth(), 
        d.getUTCDate() + daysToAdd, 
        d.getUTCHours(), 
        d.getUTCMinutes(), 
        d.getUTCSeconds(), 
        d.getUTCMilliseconds());
     };

Example: var d = new Date("5 Feb 2011").addWeekDays(1); //This should return 1 (Monday)

0

精彩评论

暂无评论...
验证码 换一张
取 消