Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. input date = "9-DEC-2011";
  2. No. of days to add = '13';
  3.  
  4. next date should be "27-Dec-2011"
  5.  
  6. var startDate = "9-DEC-2011";
  7. startDate = new Date(startDate.replace(/-/g, "/"));
  8. var endDate = "", noOfDaysToAdd = 13, count = 0;
  9. while(count < noOfDaysToAdd){
  10. endDate = new Date(startDate.setDate(startDate.getDate() + 1));
  11. if(endDate.getDay() != 0 && endDate.getDay() != 6){
  12. //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
  13. count++;
  14. }
  15. }
  16. alert(endDate);//You can format this date as per your requirement
  17.  
  18. function calcWorkingDays(fromDate, days) {
  19. var count = 0;
  20. while (count < days) {
  21. fromDate.setDate(fromDate.getDate() + 1);
  22. if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
  23. count++;
  24. }
  25. return fromDate;
  26. }
  27. alert(calcWorkingDays(new Date("9/DEC/2011"), 13));
  28.  
  29. <script language="javascript">
  30.  
  31. function getDateExcludeWeekends(startDay, startMonth, startYear, daysToAdd) {
  32. var sdate = new Date();
  33. var edate = new Date();
  34. var dayMilliseconds = 1000 * 60 * 60 * 24;
  35. sdate.setFullYear(startYear,startMonth,startDay);
  36. edate.setFullYear(startYear,startMonth,startDay+daysToAdd);
  37. var weekendDays = 0;
  38. while (sdate <= edate) {
  39. var day = sdate.getDay()
  40. if (day == 0 || day == 6) {
  41. weekendDays++;
  42. }
  43. sdate = new Date(+sdate + dayMilliseconds);
  44. }
  45. sdate.setFullYear(startYear,startMonth,startDay + weekendDays+daysToAdd);
  46. return sdate;
  47. }
  48.  
  49. </script>
  50.  
  51. function addWeekdays(date, weekdays) {
  52. var newDate = new Date(date.getTime());
  53. var i = 0;
  54. while (i < weekdays) {
  55. newDate.setDate(newDate.getDate() + 1);
  56. var day = newDate.getDay();
  57. if (day > 1 && day < 7) {
  58. i++;
  59. }
  60. }
  61. return newDate;
  62. }
  63. var currentDate = new Date('10/31/2014');
  64. var targetDate = addWeekdays(currentDate, 45);
  65. alert(targetDate);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement