add only weekdays to a date

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

add only weekdays to a date

Jonathan Hicks
Hi, I have a funtion that adds a given amount of days (taken from the user) and adds that to a date to get a new date.  I need to modify it so that only weekdays are included when you add the days.  For example, if you have today's date (9/26/06) and you want to add 5 days, the answer should be 10/3/06 because the weekend should not be counted.  Does anyone know how this can be done?

var numberOfDaysToAdd = 5
var mydate1 = new Date()  
mydate1.setFullYear(2006,9,26)  
mydate1.setDate(mydate1.getDate() + numberOfDaysToAdd)

Any help would be appreciated, Thanks!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four times a year.
http://www.fusionauthority.com/quarterly

Archive: http://www.houseoffusion.com/groups/Javascript/message.cfm/messageid:3222
Subscription: http://www.houseoffusion.com/groups/Javascript/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.33
Reply | Threaded
Open this post in threaded view
|

RE: add only weekdays to a date

Jim Davis
> -----Original Message-----
> From: Jonathan Hicks [mailto:[hidden email]]
> Sent: Tuesday, September 26, 2006 2:41 PM
> To: Javascript
> Subject: add only weekdays to a date
>
> Hi, I have a funtion that adds a given amount of days (taken from the
> user) and adds that to a date to get a new date.  I need to modify it so
> that only weekdays are included when you add the days.  For example, if
> you have today's date (9/26/06) and you want to add 5 days, the answer
> should be 10/3/06 because the weekend should not be counted.  Does anyone
> know how this can be done?

My Date Extension Library does this.

http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_D
ateExtensions/Index.cfm

Both the "add()" and "diff()" methods support "businessdays" as a DatePart
which should (if they're no bugs) do what you want.

After importing the library your sample code would become:

var numberOfDaysToAdd = 5
var mydate1 = new Date(2006,9,26)
mydate1.add(numberOfDaysToAdd, "businessdays", true);

Note that the "true" at the end is needed to change the date "in place" (a
destructive operation).  If left out (or false) the function would not
change the original date but would rather return the new date as in:

myNewDate = mydate1.add(numberOfDaysToAdd, "businessdays");

The library also provides CF-like dateFormat(), timeFormat() and compare()
functions as well as support for common ISO8601 dates.

Hope this helps.

Jim Davis



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four times a year.
http://www.fusionauthority.com/quarterly

Archive: http://www.houseoffusion.com/groups/Javascript/message.cfm/messageid:3223
Subscription: http://www.houseoffusion.com/groups/Javascript/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.33
Reply | Threaded
Open this post in threaded view
|

Re: add only weekdays to a date

Jonathan Hicks
Hi Jim,

I'm trying to use your code, but so far it's not working for me.  Can you tell me if I'm missing something?  I just tried a quick test using my example, first printing out the current date and then the date after adding 5 days.  Today is the Wed, the 27, so adding 5 business days to that should make it Wednesday, Oct 4....but it comes back with monday, Oct 2... so that means it's still looking at the weekend!  Here's my code:  

var numberOfDaysToAdd = 5
var mydate1 = new Date()
document.write(mydate1+"<br><br>")
mydate1.add(numberOfDaysToAdd, "businessdays", true);
document.write(mydate1)

What am I doing wrong?  Thanks, Jon


>> -----Original Message-----
>
>My Date Extension Library does this.
>
>http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_D
>ateExtensions/Index.cfm
>
>Both the "add()" and "diff()" methods support "businessdays" as a DatePart
>which should (if they're no bugs) do what you want.
>
>After importing the library your sample code would become:
>
>var numberOfDaysToAdd = 5
>var mydate1 = new Date(2006,9,26)
>mydate1.add(numberOfDaysToAdd, "businessdays", true);
>
>Note that the "true" at the end is needed to change the date "in place" (a
>destructive operation).  If left out (or false) the function would not
>change the original date but would rather return the new date as in:
>
>myNewDate = mydate1.add(numberOfDaysToAdd, "businessdays");
>
>The library also provides CF-like dateFormat(), timeFormat() and compare()
>functions as well as support for common ISO8601 dates.
>
>Hope this helps.
>
>Jim Davis

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four times a year.
http://www.fusionauthority.com/quarterly

Archive: http://www.houseoffusion.com/groups/Javascript/message.cfm/messageid:3235
Subscription: http://www.houseoffusion.com/groups/Javascript/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.33
Reply | Threaded
Open this post in threaded view
|

Re: add only weekdays to a date

Jonathan Hicks
In reply to this post by Jim Davis
Hi Jim,

(I just trying put up a post, but it doesnt look it went, so im resending it...sorry if it goes up twice)

I'm having trouble with the add() function.  I got the diff() function to work correctly, but it seems like there might be a bug with the add() function (unless i'm doing something wrong, which could be the case haha).  i'm trying to add 5 days to today's date (9/27/06).  Only business days should make the new date be Wednesday, Oct 4.  But instead it says monday, Oct 2.  So it seems that it still is counting weekends.  If I put in to add 6 days however, the function works correctly.  It seems that it still counts the weekends when you add certain amounts of days.  Do you have any idea how this could be fixed?

var numberOfDaysToAdd = 5
var mydate = new Date()
document.write("<br>"+mydate+"<br><br>")
mydate.add(numberOfDaysToAdd, "businessdays", true);
document.write(mydate)

Thanks for the help, Jon


>> -----Original Message-----
>
>My Date Extension Library does this.
>
>http://www.depressedpress.com/Content/Development/JavaScript/Extensions/DP_D
>ateExtensions/Index.cfm
>
>Both the "add()" and "diff()" methods support "businessdays" as a DatePart
>which should (if they're no bugs) do what you want.
>
>After importing the library your sample code would become:
>
>var numberOfDaysToAdd = 5
>var mydate1 = new Date(2006,9,26)
>mydate1.add(numberOfDaysToAdd, "businessdays", true);
>
>Note that the "true" at the end is needed to change the date "in place" (a
>destructive operation).  If left out (or false) the function would not
>change the original date but would rather return the new date as in:
>
>myNewDate = mydate1.add(numberOfDaysToAdd, "businessdays");
>
>The library also provides CF-like dateFormat(), timeFormat() and compare()
>functions as well as support for common ISO8601 dates.
>
>Hope this helps.
>
>Jim Davis

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four times a year.
http://www.fusionauthority.com/quarterly

Archive: http://www.houseoffusion.com/groups/Javascript/message.cfm/messageid:3226
Subscription: http://www.houseoffusion.com/groups/Javascript/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.33
Reply | Threaded
Open this post in threaded view
|

Re: add only weekdays to a date

Jonathan Hicks
In reply to this post by Jonathan Hicks
After trying a few different ways, I finally sat down and just wrote out my own function to do what I wanted it to.  Below is my example code that works (as far as I know so far).  It takes the current date and adds a number of days to it (in this case, it is 10 days) and it only counts weekdays.  I didn't exclude holidays, but I assume this could be added in by using an array of dates that you must statically build.  Hope this helps someone from having to do as much searching as I did!

var dueDate = 10  //number of days to add
var daysToAdd = 0
var mydate = new Date()
document.write(mydate+"<br><br>")
var day = mydate.getDay()
dueDate = dueDate - (5-day)
if ((5-day) < dueDate || dueDate == 1) {
  daysToAdd = (5-day) + 2 + daysToAdd
} else { // (5-day) >= dueDate
  daysToAdd = (5-day) + daysToAdd
}
while (dueDate != 0) {
  var week = dueDate - 5
  if (week > 0) {
    daysToAdd = 7 + daysToAdd
    dueDate = dueDate - 5
  } else { // week < 0
    daysToAdd = (5 + week) + daysToAdd
    dueDate = dueDate - (5 + week)
  }
}
mydate.setDate(mydate.getDate() + daysToAdd)
document.write(mydate)

                       
-Jon


> Hi, I have a funtion that adds a given amount of days (taken from the
> user) and adds that to a date to get a new date.  I need to modify it
> so that only weekdays are included when you add the days.  For example,
> if you have today's date (9/26/06) and you want to add 5 days, the
> answer should be 10/3/06 because the weekend should not be counted.  
> Does anyone know how this can be done?
>
> var numberOfDaysToAdd = 5
> var mydate1 = new Date()  
> mydate1.setFullYear(2006,9,26)  
> mydate1.setDate(mydate1.getDate() + numberOfDaysToAdd)
>
> Any help would be appreciated,
Thanks!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four times a year.
http://www.fusionauthority.com/quarterly

Archive: http://www.houseoffusion.com/groups/Javascript/message.cfm/messageid:3233
Subscription: http://www.houseoffusion.com/groups/Javascript/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.33
Reply | Threaded
Open this post in threaded view
|

RE: add only weekdays to a date

Jim Davis
In reply to this post by Jonathan Hicks
> -----Original Message-----
> From: Jonathan Hicks [mailto:[hidden email]]
> Sent: Wednesday, September 27, 2006 10:26 AM
> To: Javascript
> Subject: Re: add only weekdays to a date
>
> Hi Jim,
>
> (I just trying put up a post, but it doesnt look it went, so im resending
> it...sorry if it goes up twice)
>
> I'm having trouble with the add() function.  I got the diff() function to
> work correctly, but it seems like there might be a bug with the add()
> function (unless i'm doing something wrong, which could be the case haha).
> i'm trying to add 5 days to today's date (9/27/06).  Only business days
> should make the new date be Wednesday, Oct 4.  But instead it says monday,

Well... that's not suprising - that function was horribly broken.  ;^)

Well. Maybe not "horribly" - but lordy, it weren't good.

I've fixed it and uploaded the new version.

Let me know if I've fouled up again.

Jim Davis



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Introducing the Fusion Authority Quarterly Update. 80 pages of hard-hitting,
up-to-date ColdFusion information by your peers, delivered to your door four times a year.
http://www.fusionauthority.com/quarterly

Archive: http://www.houseoffusion.com/groups/Javascript/message.cfm/messageid:3236
Subscription: http://www.houseoffusion.com/groups/Javascript/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=17837.14401.33