Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

The beginning or end of previous week

Total Hit ( 3813)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


For reporting, many times you need to figure out what date last week began and ended. Use the code below to figure that out:

Click here to copy the following block
Dim vntBegin As variant
Dim vntEnd As variant
Const constSunday As Integer = 1
GetPriorWorkWeek BeginDayOfWeek:=constSunday, WeekBegin:=vntBegin, _
  WeekEnd:=vntEnd
Print "Begin of Week: " & vtnBegin & ", End of Week: " & vtnEnd

This is the complete code of the GetPriorWorkWeek routine. You can easily modify it to work with any date passed as an argument, not just the current system date:
' Determine the date of the beginning and end of a week based on today
' and the day the week begins on
'
' Input    : BeginDayOfWeek: 1 = Sunday thru 7 = Saturday
' Input/Output : WeekBegin: It sets this to the first day of prior work week
'        WeekEnd: It sets this to the last day of the prior work week

Sub GetPriorWorkWeek(ByVal BeginDayOfWeek As Integer, Optional ByVal WeekBegin _
  As Variant, Optional ByVal WeekEnd As Variant)

  Dim dteDate As Date
  Dim blnFound As Boolean

  If BeginDayOfWeek < 1 Or BeginDayOfWeek > 7 Then
   Err.Raise Number:=1000, Source:="GetLastDayOfMonth", _
     Description:="Invalid BeginDayOfWeek, must be between 1 and 7"
  End If

  'Subtract 7 days ago to arrive at beginning date
  dteDate = DateAdd("d", -7, Now)

  Do Until blnFound
   'If on the beginning of the week, stop;
   'otherwise go back until the first day of the week is found
   If Weekday(dteDate) = BeginDayOfWeek Then
     WeekBegin = dteDate
     blnFound = True
   Else
     dteDate = DateAdd("d", -1, dteDate)
   End If
  Loop

  WeekEnd = DateAdd("d", 6, WeekBegin)

End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.