Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input date = "9-DEC-2011";
- No. of days to add = '13';
- next date should be "27-Dec-2011"
- var startDate = "9-DEC-2011";
- startDate = new Date(startDate.replace(/-/g, "/"));
- var endDate = "", noOfDaysToAdd = 13, count = 0;
- while(count < noOfDaysToAdd){
- endDate = new Date(startDate.setDate(startDate.getDate() + 1));
- if(endDate.getDay() != 0 && endDate.getDay() != 6){
- //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
- count++;
- }
- }
- alert(endDate);//You can format this date as per your requirement
- function calcWorkingDays(fromDate, days) {
- var count = 0;
- while (count < days) {
- fromDate.setDate(fromDate.getDate() + 1);
- if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
- count++;
- }
- return fromDate;
- }
- alert(calcWorkingDays(new Date("9/DEC/2011"), 13));
- <script language="javascript">
- function getDateExcludeWeekends(startDay, startMonth, startYear, daysToAdd) {
- var sdate = new Date();
- var edate = new Date();
- var dayMilliseconds = 1000 * 60 * 60 * 24;
- sdate.setFullYear(startYear,startMonth,startDay);
- edate.setFullYear(startYear,startMonth,startDay+daysToAdd);
- var weekendDays = 0;
- while (sdate <= edate) {
- var day = sdate.getDay()
- if (day == 0 || day == 6) {
- weekendDays++;
- }
- sdate = new Date(+sdate + dayMilliseconds);
- }
- sdate.setFullYear(startYear,startMonth,startDay + weekendDays+daysToAdd);
- return sdate;
- }
- </script>
- function addWeekdays(date, weekdays) {
- var newDate = new Date(date.getTime());
- var i = 0;
- while (i < weekdays) {
- newDate.setDate(newDate.getDate() + 1);
- var day = newDate.getDay();
- if (day > 1 && day < 7) {
- i++;
- }
- }
- return newDate;
- }
- var currentDate = new Date('10/31/2014');
- var targetDate = addWeekdays(currentDate, 45);
- alert(targetDate);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement