PC Review


Reply
Thread Tools Rate Thread

Calculate week from current day

 
 
Robert Crandal
Guest
Posts: n/a
 
      4th Dec 2009
This is probably an easy question, but i'll ask anyhow:

One of my subroutines is passed the current date in "mm-dd-yy"
format. For example, it would pass in a value of "12-04-09"
for today's date.

I now need some code that will give me the Sunday to Saturday
date range that coincides with the current date. Therefore,
the code should give me a start date of "11-29-09" and end
date of "12-05-09" because "12-04-09" is part of that week.

thanx again

 
Reply With Quote
 
 
 
 
toon_24
Guest
Posts: n/a
 
      4th Dec 2009
Hello Robert

you can use the function "Weekday"

Dim MyDate, MyWeekDay
MyDate = #12 februari 1969#
MyWeekDay = Weekday(MyDate)





"Robert Crandal" wrote:

> This is probably an easy question, but i'll ask anyhow:
>
> One of my subroutines is passed the current date in "mm-dd-yy"
> format. For example, it would pass in a value of "12-04-09"
> for today's date.
>
> I now need some code that will give me the Sunday to Saturday
> date range that coincides with the current date. Therefore,
> the code should give me a start date of "11-29-09" and end
> date of "12-05-09" because "12-04-09" is part of that week.
>
> thanx again
>
> .
>

 
Reply With Quote
 
Cheetahke
Guest
Posts: n/a
 
      4th Dec 2009
Hello Robert,

you can use the function "Weekday"

Dim MyDate, MyWeekDay, Day1, Day2, Day3, Day4, Day5, Day6, Day7
MyDate = #12 februari 1969#
MyWeekDay = Weekday(MyDate)

Select case MyWeekDay
case 1
Day1 = MyDate
Day2 = MyDate + 1
Day3 = MyDate + 2
(and so on)
case 2
Day1 = MyDate - 1
Day2 = MyDate
Day3 = MyDate + 1
....
case 3
Day1 = MyDate - 2
....
case 4
....
end Select

"Robert Crandal" wrote:

> This is probably an easy question, but i'll ask anyhow:
>
> One of my subroutines is passed the current date in "mm-dd-yy"
> format. For example, it would pass in a value of "12-04-09"
> for today's date.
>
> I now need some code that will give me the Sunday to Saturday
> date range that coincides with the current date. Therefore,
> the code should give me a start date of "11-29-09" and end
> date of "12-05-09" because "12-04-09" is part of that week.
>
> thanx again
>
> .
>

 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      4th Dec 2009
On Fri, 4 Dec 2009 03:58:22 -0700, "Robert Crandal" <(E-Mail Removed)> wrote:

>This is probably an easy question, but i'll ask anyhow:
>
>One of my subroutines is passed the current date in "mm-dd-yy"
>format. For example, it would pass in a value of "12-04-09"
>for today's date.
>
>I now need some code that will give me the Sunday to Saturday
>date range that coincides with the current date. Therefore,
>the code should give me a start date of "11-29-09" and end
>date of "12-05-09" because "12-04-09" is part of that week.
>
>thanx again


Here's one way to get those two dates:

================================
Option Explicit
Sub WeekRange()
Dim dt As Date
dt = #12/4/2009#
Debug.Print WeekDates(dt)(0), WeekDates(dt)(1)
End Sub
'------------------------------------------------
Function WeekDates(dt As Date) As Variant
Dim StartDt As Date, EndDt As Date
Dim sTemp(0 To 1)

StartDt = dt - Weekday(dt, vbMonday)
EndDt = dt + 7 - Weekday(dt, vbSunday)

sTemp(0) = StartDt
sTemp(1) = EndDt

WeekDates = sTemp
End Function
====================================
--ron
 
Reply With Quote
 
Member
Join Date: Sep 2009
Posts: 45
 
      4th Dec 2009
if you are satisfied with formula instead of a code
date is in A1

=A1-WEEKDAY(A1)+1
will give you beginning of the week

=A1+7-WEEKDAY(A1)
will give you the last day of the week (week is from Sunday to Saturday)

try some experiments
 
Reply With Quote
 
Sensetech
Guest
Posts: n/a
 
      4th Dec 2009
A little more concise ...

Dim MyDate, MyWeekDay, Day1, Day2, Day3, Day4, Day5, Day6, Day7
MyDate = #12 februari 1969#
MyWeekDay = Weekday(MyDate)

Day1= MyDate - Choose(Weekday(MyDate),0,1,2,3,4,5,6)
Day2 = Day1 + 1
Day3 = Day1 + 2
Day4 = Day1 + 3

etc.

--
Learning something new & wonderful every day ...


"Robert Crandal" wrote:

> This is probably an easy question, but i'll ask anyhow:
>
> One of my subroutines is passed the current date in "mm-dd-yy"
> format. For example, it would pass in a value of "12-04-09"
> for today's date.
>
> I now need some code that will give me the Sunday to Saturday
> date range that coincides with the current date. Therefore,
> the code should give me a start date of "11-29-09" and end
> date of "12-05-09" because "12-04-09" is part of that week.
>
> thanx again
>
> .
>

 
Reply With Quote
 
Robert Crandal
Guest
Posts: n/a
 
      4th Dec 2009
Thank you everyone who responded to this thread.
You are all awesome!!!!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Current year and current week number Grey Old Man Microsoft Excel Misc 9 8th Dec 2009 06:30 PM
current week, current month, current year joemeshuggah Microsoft Excel Programming 1 14th Oct 2008 06:44 PM
Auto calculate day of week to week of the year (not as serial) oftenconfused Microsoft Excel Misc 4 23rd Jun 2008 05:14 PM
How do I make my calendar open to the current week not the current Pat Microsoft Outlook Calendar 0 28th Mar 2008 09:03 PM
How can the current week be the default starting week in Month View? Howie Microsoft Outlook 0 9th Mar 2005 06:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:42 PM.