473,803 Members | 3,410 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 3387
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(DateSer ial(Year(dtD), Month(dtD), 1), 2) + D)
Mod 7 + 1 + (N - 1) * 7
End Function

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

intFirstSunday = NthXDay(1, vbSunday, DateSerial(intY ear, 1, 1))
dtFirstSunday = DateSerial(intY ear, 1, intFirstSunday)
dtSaturday = GetSaturday(int Week, intYear)
If Day(dtFirstSund ay) = 1 Then
GetSunday = DateAdd("d", -6, dtSaturday)
Else
GetSunday = DateAdd("d", 1, dtSaturday)
End If
End Function

Public Function GetSaturday(int Week As Integer, intYear As Integer) As
Variant
Dim intFirstSaturda y As Integer
Dim dtFirstSaturday As Date

intFirstSaturda y = NthXDay(1, vbSaturday, DateSerial(intY ear, 1, 1))
dtFirstSaturday = DateSerial(intY ear, 1, intFirstSaturda y)
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(intWe ek As Integer, intYear As Integer) As
Variant
Dim dtSaturday As Variant

dtSaturday = GetSaturday(int Week, intYear)
If Month(dtSaturda y) = 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(DateSer ial(Year(dtD), Month(dtD), 1), 2) + D)
Mod 7 + 1 + (N - 1) * 7
End Function

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

intFirstSunday = NthXDay(1, vbSunday, DateSerial(intY ear, 1, 1))
dtFirstSunday = DateSerial(intY ear, 1, intFirstSunday)
dtSaturday = GetSaturday(int Week, intYear)
If Day(dtFirstSund ay) = 1 Then
GetSunday = DateAdd("d", -6, dtSaturday)
Else
GetSunday = DateAdd("d", 1, dtSaturday)
End If
End Function

Public Function GetSaturday(int Week As Integer, intYear As Integer) As
Variant
Dim intFirstSaturda y As Integer
Dim dtFirstSaturday As Date

intFirstSaturda y = NthXDay(1, vbSaturday, DateSerial(intY ear, 1, 1))
dtFirstSaturday = DateSerial(intY ear, 1, intFirstSaturda y)
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(intWe ek As Integer, intYear As Integer) As
Variant
Dim dtSaturday As Variant

dtSaturday = GetSaturday(int Week, intYear)
If Month(dtSaturda y) = 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
3213
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 variable that will store the date this past Monday (even if today happens to be Monday) also in YYY-MM-DD format. So, the end result is that, for this week (today is 2004-08-18) I would have the following two variables:
4
6608
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 varchar(25) SQL statement: SELECT DISTINCT FileDate
8
2243
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 to have all the prior dates pop in. Example: User enters 2/9/05 in the date field at the top being a Wednesday...I need the dates at the bottom to be filled in automatically, respectively as 2/3/05, 2/4/05, 2/5/05, 2/6/05, 2/7/05, 2/8/05, 2/9/05...
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
16689
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 Sunday->Saturday range rather than a Monday->Sunday range. Thus, >>> startDate, stopDate = weekBoundaries(2006, 23) would yield a start-date of June 4, 2006 and an end-date of June
4
2382
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 Mondays, I have to enter 3 days of data - Friday, Saturday, and Sunday. Last Monday, an employee came to me questioning the results on his report. He had requested data from the weekend: Friday through Sunday. But when he printed the report, I had...
6
2563
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. Please help me out
1
3881
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 problem here is the QTY will not be inserted on sundays. When when we run the query on a Monday, we should consider the Qty for last wednesday to saturday and for monday. I have written a query where I retrieve the data for last 6 days considering...
3
2356
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.. that is i have dates from sunday to saturday and I have to find which date is sunday......
0
9700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.