By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

Any function can return the date (bypassing weeknumber,weekyday) ?

P: n/a
Does .net provide any function for such purpose ?
e.g today is 21-10-2004, the week is 43, now I want to get the DATE with
(week 42, Monday)
Thanks alot
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Agnes,
You can use DateTime.AddDays(7) to add a week or AddDays(-7) to subtract a
week from a specific date.

Dim today As DateTime = #10/21/2004#
lastWeek = today.AddDays(7)

If you want to adjust a date to a specific day of week you can use something
like:

Private Shared Function FirstDayOfWeek(ByVal day As DateTime, ByVal
weekStarts As DayOfWeek) As DateTime
Return day.AddDays(weekStarts - day.DayOfWeek)
End Function

Public Shared Sub Main()
Dim today As DateTime = #10/21/2004#
Dim lastWeek As DateTime = today.AddDays(-7)
Dim nextWeek As DateTime = today.AddDays(7)

Dim lastMonday As DateTime = FirstDayOfWeek(lastWeek,
DayOfWeek.Monday)
Dim nextMonday As DateTime = FirstDayOfWeek(nextWeek,
DayOfWeek.Monday)

Debug.WriteLine(today, "today")
Debug.WriteLine(lastWeek, "last week")
Debug.WriteLine(lastMonday, "last monday")
Debug.WriteLine(nextWeek, "next week")
Debug.WriteLine(nextMonday, "next monday")
End Sub

If you want to find a date in a specific week of the year you can use:

Public Function GetDateFromWeek(ByVal year As Integer, _
ByVal week As Integer, _
ByVal dayOfweek As DayOfWeek) Ad Date

Dim firstOfYear As New Date(year, 1, 1)

Return firstOfYear.AddDays((week - 1) * 7 + dayOfWeek _
- firstOfYear.DayOfWeek)

End Function

Then you can call it as:

Dim week As Date
week = GetDateFromWeek(2004, 42, DayOfWeek.Monday)

I will leave calculating the Week Of Year function for you.

Remember that Date is simply an alias for DateTime.

Hope this helps
Jay

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Does .net provide any function for such purpose ?
e.g today is 21-10-2004, the week is 43, now I want to get the DATE with
(week 42, Monday)
Thanks alot

Nov 21 '05 #2

P: n/a
Thanks Jay,
I know how to write a function 'week of year' , so I try to combine your
code . Thanks

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> bl
news:uf**************@TK2MSFTNGP11.phx.gbl g...
Agnes,
You can use DateTime.AddDays(7) to add a week or AddDays(-7) to subtract a
week from a specific date.

Dim today As DateTime = #10/21/2004#
lastWeek = today.AddDays(7)

If you want to adjust a date to a specific day of week you can use something like:

Private Shared Function FirstDayOfWeek(ByVal day As DateTime, ByVal
weekStarts As DayOfWeek) As DateTime
Return day.AddDays(weekStarts - day.DayOfWeek)
End Function

Public Shared Sub Main()
Dim today As DateTime = #10/21/2004#
Dim lastWeek As DateTime = today.AddDays(-7)
Dim nextWeek As DateTime = today.AddDays(7)

Dim lastMonday As DateTime = FirstDayOfWeek(lastWeek,
DayOfWeek.Monday)
Dim nextMonday As DateTime = FirstDayOfWeek(nextWeek,
DayOfWeek.Monday)

Debug.WriteLine(today, "today")
Debug.WriteLine(lastWeek, "last week")
Debug.WriteLine(lastMonday, "last monday")
Debug.WriteLine(nextWeek, "next week")
Debug.WriteLine(nextMonday, "next monday")
End Sub

If you want to find a date in a specific week of the year you can use:

Public Function GetDateFromWeek(ByVal year As Integer, _
ByVal week As Integer, _
ByVal dayOfweek As DayOfWeek) Ad Date

Dim firstOfYear As New Date(year, 1, 1)

Return firstOfYear.AddDays((week - 1) * 7 + dayOfWeek _
- firstOfYear.DayOfWeek)

End Function

Then you can call it as:

Dim week As Date
week = GetDateFromWeek(2004, 42, DayOfWeek.Monday)

I will leave calculating the Week Of Year function for you.

Remember that Date is simply an alias for DateTime.

Hope this helps
Jay

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Does .net provide any function for such purpose ?
e.g today is 21-10-2004, the week is 43, now I want to get the DATE with
(week 42, Monday)
Thanks alot


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.