473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find Dates For Sunday and Saturday

How do I find the dates of the Sunday and Saturday of a week if I have the
week number of a selected year?

Thanks!

Jerry

Nov 13 '05 #1
3 3369
If each week is exactly seven days starting January 1, then you can use
the following functions:
'-----------
Public Function NthXDay(N As Integer, D As Integer, dtD As Date) As
Integer
NthXDay = (6 - WeekDay(DateSerial(Year(dtD), Month(dtD), 1), 2) + D)
Mod 7 + 1 + (N - 1) * 7
End Function

Public Function GetSunday(intWeek As Integer, intYear As Integer) As
Variant
Dim intFirstSunday As Integer
Dim dtFirstSunday As Date
Dim dtSaturday As Variant

intFirstSunday = NthXDay(1, vbSunday, DateSerial(intYear, 1, 1))
dtFirstSunday = DateSerial(intYear, 1, intFirstSunday)
dtSaturday = GetSaturday(intWeek, intYear)
If Day(dtFirstSunday) = 1 Then
GetSunday = DateAdd("d", -6, dtSaturday)
Else
GetSunday = DateAdd("d", 1, dtSaturday)
End If
End Function

Public Function GetSaturday(intWeek As Integer, intYear As Integer) As
Variant
Dim intFirstSaturday As Integer
Dim dtFirstSaturday As Date

intFirstSaturday = NthXDay(1, vbSaturday, DateSerial(intYear, 1, 1))
dtFirstSaturday = DateSerial(intYear, 1, intFirstSaturday)
GetSaturday = DateAdd("ww", intWeek - 1, dtFirstSaturday)
End Function
'-----------

If you assume that each week begins on a Sunday (i.e., January 1, 2005
is the end of the first week) then change GetSunday to:
'-----------
Public Function GetSunday(intWeek As Integer, intYear As Integer) As
Variant
Dim dtSaturday As Variant

dtSaturday = GetSaturday(intWeek, intYear)
If Month(dtSaturday) = 1 And Day(dtSaturday) < 7 Then
GetSunday = Null
Else
GetSunday = DateAdd("ww", -1, dtSaturday)
End If
End Function
'-----------

Test to your satisfaction before using this code. Option 1 can use
Dates instead of Variants since there's no Null situation.
James A. Fortune

Nov 13 '05 #2
If each week is exactly seven days starting January 1, then you can use
the following functions:
'-----------
Public Function NthXDay(N As Integer, D As Integer, dtD As Date) As
Integer
NthXDay = (6 - WeekDay(DateSerial(Year(dtD), Month(dtD), 1), 2) + D)
Mod 7 + 1 + (N - 1) * 7
End Function

Public Function GetSunday(intWeek As Integer, intYear As Integer) As
Variant
Dim intFirstSunday As Integer
Dim dtFirstSunday As Date
Dim dtSaturday As Variant

intFirstSunday = NthXDay(1, vbSunday, DateSerial(intYear, 1, 1))
dtFirstSunday = DateSerial(intYear, 1, intFirstSunday)
dtSaturday = GetSaturday(intWeek, intYear)
If Day(dtFirstSunday) = 1 Then
GetSunday = DateAdd("d", -6, dtSaturday)
Else
GetSunday = DateAdd("d", 1, dtSaturday)
End If
End Function

Public Function GetSaturday(intWeek As Integer, intYear As Integer) As
Variant
Dim intFirstSaturday As Integer
Dim dtFirstSaturday As Date

intFirstSaturday = NthXDay(1, vbSaturday, DateSerial(intYear, 1, 1))
dtFirstSaturday = DateSerial(intYear, 1, intFirstSaturday)
GetSaturday = DateAdd("ww", intWeek - 1, dtFirstSaturday)
End Function
'-----------

If you assume that each week begins on a Sunday (i.e., January 1, 2005
is the end of the first week) then change GetSunday to:
'-----------
Public Function GetSunday(intWeek As Integer, intYear As Integer) As
Variant
Dim dtSaturday As Variant

dtSaturday = GetSaturday(intWeek, intYear)
If Month(dtSaturday) = 1 And Day(dtSaturday) < 7 Then
GetSunday = Null
Else
GetSunday = DateAdd("ww", -1, dtSaturday)
End If
End Function
'-----------

Test to your satisfaction before using this code. Option 1 can use
Dates instead of Variants since there's no Null situation.
James A. Fortune

Nov 13 '05 #3

Jerry wrote:
How do I find the dates of the Sunday and Saturday of a week if I have the week number of a selected year?

Thanks!

Jerry


Sorry about the multiple posts. Google was lying when they claimed a
communications error kept the reply from being posted.

James A. Fortune

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3198
by: Eric Linders | last post by:
Hello, Today is Thursday, August 18, 2004. I would like to have a variable that stores the date this coming Sunday (even if today happened to be Sunday) in YYY-MM-DD format. I also need a...
4
6586
by: SQLJunkie | last post by:
Here is an issue that has me stumped for the past few days. I have a table called MerchTran. Among various columns, the relevant columns for this issue are: FileDate datetime , SourceTable...
8
2220
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need...
0
291
by: Jerry | last post by:
Given a week number of a selected year, how do I find the dates of the Sunday and Saturday of that week? Thanks! Jerry
3
16643
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
4
2374
by: jdph40 | last post by:
I enter data into a database that resides on our network. Other employees can print reports from the database, but they have no way of telling if I've entered all the data. For instance, on...
6
2550
RamananKalirajan
by: RamananKalirajan | last post by:
Can any one please help me out in calculating the no of weekdays between two givendays using Javascripts I tried in many way but I cant get the solution. The date format I am using is DD-MON-YYYY....
1
3845
by: Pradeepa01 | last post by:
Hi, We are facing an issue with the JDBC query, I need help on. I have 3 fields, UPC_ID, QTY,Date I have to retrieve all the records for one UPC_ID whose QTY is same for last five days. The...
3
2345
by: pchaitanya | last post by:
I have selected some list of valid dates to a label. now i need to find first day among the given dates from label contrl i got dates from calender control by clicking for entire week.. ...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.