House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  Javascript

add only weekdays to a date

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jonathan Hicks
09/26/2006 02:52 PM

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!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jim Davis
09/26/2006 04:16 PM

> 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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jonathan Hicks
09/27/2006 10:35 AM

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 ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jim Davis
09/28/2006 09:40 PM

----- Excess quoted text cut - see Original Post for more ----- 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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jonathan Hicks
09/28/2006 03:41 PM

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 ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jonathan Hicks
09/28/2006 10:16 AM

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 ----- Excess quoted text cut - see Original Post for more ----- Thanks!


<< Previous Thread Today's Threads Next Thread >>

Search javascript

October 23, 2014

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31   

Designer, Developer and mobile workflow conference