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:
You can create
Date
objects for specific dates using the three-argument constructor function:Date(year, month, date)
whereyear
is the full year (e.g., 2011),month
is the month number (0
= January), anddate
is the day of the month (1-28/29/30/31 depending). E.g.:var dt = new Date(2011, 0, 31); // January 31st, 2011
You can tell what day of the week a given
Date
represents by using itsgetDay
function; that returns a number with0
= 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 }
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.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 anid
attribute (which must be unique) and then usevar element = document.getElementById("idstring");
...to look it up.
- 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)
精彩评论