Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding weekdays to a date 1

Status
Not open for further replies.

newbie404

Programmer
Mar 2, 2004
19
IE
Hi,

I am writing an ASP program but need to have some javascript in it too. I need a javascript function that you can supply in a date in the format mm/dd/yyyy and an integer which is the number of weekdays (M-F) to add to it.I have written the function for use in the ASP side but am a bit stumped with javascript.

Then I started thinking that this must be a common enough code snippet so maybe someone has it. (Ever the optimist!). Even if not, would anyone be able to give me some pointers on hw to go about this in javascript?

thanks,
A
 
Thanks Dan, but not exactly. My problem is with weekdays, or adding a day. I got a snippet which I have changed so I have...

function MoveWeekdays(num_days) {
//get date from form
dt=document.myform.myfield.value
dt=dt.split("/")
datField=new Date()

dd=parseInt(dt[1])//The Date
mm=parseInt(dt[0])//The Month
yy=parseInt(dt[2])//The Year

datField.setDate(dd)
datField.setMonth(mm)
datField.setYear(yy)

// advance num_days weekdays
moved_by = 0;
while ( moved_by < num_days )
{
datField.add( "day", 1 );
// skip over any weekend days
while ( ! datField.isWeekday() )
datField.add( "day", 1 );
moved_by++;
}
document.write(datField);
}



The problem is twofold with this. First there is no .add for dates in JS as far as I know...

Second is that it doesn't work! I have tried this in a browser and get a error;

<HTML><HEAD><TITLE></TITLE>

function MoveWeekdays(num_days) {

dt="7/21/2004"; //document.FormName.TheTextField.value
dt=dt.split("/")
datField=new Date()

dd=parseInt(dt[1])//The Textbox Date
mm=parseInt(dt[0])//The Textbox Month
yy=parseInt(dt[2])//The Textbox Year

datField.setDate(dd)
datField.setMonth(mm-1)
datField.setYear(yy)

// advance num_days weekdays
moved_by = 0;
while ( moved_by < num_days )
{
datField.add( "day", 1 );
// skip over any weekend days
while ( ! start_date.isWeekday() )
start_date.add( "day", 1 );
moved_by++;
}
document.write(datField);
}


</SCRIPT>

</HEAD><BODY onLoad='TestThis( 7)'></BODY></HTML>



Any ideas?
 
>> Second is that it doesn't work!

That's because of point 1 (no add method :eek:)

You can use "dateObj.getDay();" to check to see if a date is a weekend or not, so replace this:

Code:
while ( ! start_date.isWeekday() )

with this:

Code:
while ( start_date.getDay() == 0 || start_date.getDay() == 6 )

Obviously, this assumes you have actually defined a date object called "start_date" somewhere.

To add 1 day to a date object, you can use this:

Code:
dateObj.setTime(dateObj.getTime() + 86400000);

As there are 86400000 milliseconds in 1 day.

For a full list of date object properties, methods, etc, check this MSDN link out:
Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top