?
Solved

DateAdd (with minutes) to skip weekends in classic ASP

Posted on 2012-03-30
2
Medium Priority
?
862 Views
Last Modified: 2012-04-02
I'm doing some development in classic ASP.  I have a starting datetime value and I want to add to it a period of time in minutes, but it needs to skip weekends.

For example, if I have a starting datetime of Thursday @ 7:00am and I add to it 3045 minutes (50 hours: 45 minutes)...it should finish on Monday @ 9:45am.

Any ideas?

Thanks.
bruno71
0
Comment
Question by:bruno71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 37788556
I'm sure there is more then one way to do this.  I would start with finding the end date, then looping through each day to find which is a weekend day, then add the minutes for those days.  If you need to account for the start date being a weekend, then find the time it starts and subtract those minutes.  I have not tested this.  

'Assumes your start date is not on a weekend.
StartDate=request("StartDate") 'format should be in something like, "1/1/2012 08:50:00"
MinutesAdded=request("MinutesAdded")
EndDate = dateadd("n", MinutesAdded,StartDate) ' find the end date
totDays=datediff("d",StartDate,EndDate) ' find out how many days you are adding.

WeekEndCounter =0
for x = 1 to totDays
   if Weekday(DateAdd("d",x,StartDate) )=1 or Weekday(DateAdd("d",x,StartDate) )=7 then ' check for sunday or saturday
   WeekEndCounter=WeekEndCounter+1
next
AddMoreMinutes=WeekEndCounter*1440 ' 1440 minutes in a day ' could have just done days too for this
FinalDateTime = dateadd("n",AddMoreMinutes,DateEnd)

' If you need to account for the start date being a weekend, then we need to subtract some minutes.
If weekday(StartDate) = 1 or weekday(StartDate) = 7 then
     SubtractMinutes = ((hour(DateStart)*60 ) + minute(DateStart)) * -1
     FinalDateTime=dateadd("n",SubtractMinutes,FinalDateTime)

end if

Open in new window

0
 

Author Closing Comment

by:bruno71
ID: 37797422
Thanks.  That worked...except I had to add one little piece.

This is to account for the recalculated ending date being on a weekend...

Do While Weekday(EndDate) = 1 or Weekday(EndDate) = 7
            EndDate = DateAdd("n", 1440, EndDate)
Loop

For example, if the ending date landed on a Tuesday and we skipped two weekends...the calculation would add 4 days - landing on a Saturday.  This just loops through to the next weekday.
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question