Click here to Skip to main content
15,902,275 members
Home / Discussions / Web Development
   

Web Development

 
Generalshould JMF be installed in all client PC Pin
karthik prasanna20-Jul-04 3:08
karthik prasanna20-Jul-04 3:08 
GeneralAdding number of days to a Date Pin
Ph@ntom19-Jul-04 20:34
Ph@ntom19-Jul-04 20:34 
GeneralRe: Adding number of days to a Date Pin
Alexander Wiseman20-Jul-04 4:29
Alexander Wiseman20-Jul-04 4:29 
GeneralRe: Adding number of days to a Date Pin
Ph@ntom20-Jul-04 16:59
Ph@ntom20-Jul-04 16:59 
GeneralRe: Adding number of days to a Date Pin
Ph@ntom20-Jul-04 19:13
Ph@ntom20-Jul-04 19:13 
GeneralRe: Adding number of days to a Date Pin
Alexander Wiseman21-Jul-04 3:31
Alexander Wiseman21-Jul-04 3:31 
GeneralRe: Adding number of days to a Date Pin
Ph@ntom23-Jul-04 17:48
Ph@ntom23-Jul-04 17:48 
GeneralRe: Adding number of days to a Date Pin
Alexander Wiseman25-Jul-04 3:22
Alexander Wiseman25-Jul-04 3:22 
Okay, I figured it out and this time I've tested it. The remaining problem was caused under these conditions:

1) The startDate is a Thursday or a Friday
2) The number of days being added is a multiple of 5

In this case, the algorithm would always return a Thursday or Friday because it would simple add 7*(number of times 5 goes into numDays). To fix this, all I added was another "else" clause which checked this case and fixed it. Below is the final function:
// Function DateAdd: adds a number of non-work days and a number of months and years
// to a given date
function DateAdd(startDate, numDays, numMonths, numYears)
{
var returnDate = new Date(startDate.getTime()) ;
var yearsToAdd = numYears ; 
var month = returnDate.getMonth() + numMonths;

if ( month > 11 )
{
	yearsToAdd = Math.floor((month+1)/12) ;
	month -= 12*yearsToAdd ;
	yearsToAdd += numYears ;
}

returnDate.setMonth(month) ;
returnDate.setFullYear(returnDate.getFullYear() + yearsToAdd) ;

// Before we add the days, make sure to exclude non-work days:
var finalDays = 0;
if(numDays >= 5)
{
	//Since you have 2 non-work days, you basically have a 5-day week:
	var weeks = Math.floor((numDays / 5));
	finalDays = (weeks * 7);
	numDays -= (weeks * 5);
}

// numDays is now less than 5:
var dayOfWeek = returnDate.getDay();
if(numDays > 0)
{

	// See if, adding the days, we land on or pass any non-work days:
	if(dayOfWeek < 4 && (dayOfWeek + numDays) >= 4)
	{
		finalDays += (numDays + 2); //effectively skips Thursday and Friday
	}
	else
	{
		if(dayOfWeek == 4 || dayOfWeek == 5)
		{
			finalDays += (numDays + (5-dayOfWeek));
		}
		else
			finalDays += numDays;
	}

}
else
{
	//This is the odd case: where we started on a work day and numDays is evenly divisible by 5:
	if(dayOfWeek == 4 || dayOfWeek == 5)
	{
		finalDays -= (dayOfWeek - 3);
	}
}

// Now finalDays contains the number of days we want to add:
returnDate.setTime(returnDate.getTime()+60000*60*24*finalDays);
return returnDate;

}


Hope that works for you. I apologize for the mistakes.

Sincerely,
Alexander Wiseman
GeneralRe: Adding number of days to a Date Pin
Ph@ntom25-Jul-04 6:16
Ph@ntom25-Jul-04 6:16 
GeneralRe: Adding number of days to a Date Pin
Ph@ntom26-Jul-04 6:12
Ph@ntom26-Jul-04 6:12 
GeneralRe: Adding number of days to a Date Pin
Alexander Wiseman27-Jul-04 8:00
Alexander Wiseman27-Jul-04 8:00 
GeneralRe: Adding number of days to a Date Pin
Ph@ntom27-Jul-04 19:40
Ph@ntom27-Jul-04 19:40 
GeneralRe: Adding number of days to a Date Pin
Alexander Wiseman28-Jul-04 4:31
Alexander Wiseman28-Jul-04 4:31 
GeneralRe: Adding number of days to a Date Pin
Ph@ntom30-Jul-04 18:57
Ph@ntom30-Jul-04 18:57 
GeneralRe: Adding number of days to a Date Pin
Alexander Wiseman2-Aug-04 7:15
Alexander Wiseman2-Aug-04 7:15 
GeneralCalling Web Services from a web page Pin
NiteShade19-Jul-04 6:36
NiteShade19-Jul-04 6:36 
GeneralRe: Calling Web Services from a web page Pin
VenkatFor.NET21-Jul-04 8:08
VenkatFor.NET21-Jul-04 8:08 
GeneralRe: Calling Web Services from a web page Pin
mysorian11-Aug-04 11:41
professionalmysorian11-Aug-04 11:41 
GeneralLocalHost and Browser Pin
ohdil16-Jul-04 8:41
ohdil16-Jul-04 8:41 
GeneralRe: LocalHost and Browser Pin
Javier Lozano18-Jul-04 17:01
Javier Lozano18-Jul-04 17:01 
GeneralRe: LocalHost and Browser Pin
ohdil22-Jul-04 13:40
ohdil22-Jul-04 13:40 
GeneralRe: LocalHost and Browser Pin
Javier Lozano22-Jul-04 14:18
Javier Lozano22-Jul-04 14:18 
GeneralGarbage Collection in JS Pin
abc87616-Jul-04 0:15
abc87616-Jul-04 0:15 
GeneralAWS Report for OpenListing Pin
Sumit Kapoor15-Jul-04 21:24
Sumit Kapoor15-Jul-04 21:24 
GeneralRe: AWS Report for OpenListing Pin
zahedonline30-Oct-08 3:41
zahedonline30-Oct-08 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.