473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
2 4049
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
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> ¦b¶l¥ó
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jozua | last post by:
Hi Can anybody please help. I require a user to select a date on a JavaScript calendar. Based on the date selected by the user the system must determine which YEAR and WEEKNUMBER the user has...
1
by: windandwaves | last post by:
Hi Folk Some of you may be interested in a function that allows you to add/substract from a date in PHP, using a MySql Dateformat (e.g. 2005-10-31) (NOT TESTED): Any comments appreciated. ...
3
by: dik mus | last post by:
Hi, I connect to a database that only stores the year and weeknumber. But i need the date of the corresponding monday. This could be more difficult as one expect because the weekumbers might...
1
by: Martin Emanuelsson | last post by:
Hello, Knowing year and weeknumber of this year, I'm looking for some way to get the date of the first and last day of that particular week. Does anyone have an idea about how to do this? ...
1
by: Vanessa | last post by:
Does anyone know whether when I select a weeknumber of a certain year, eg. 2004, the date from first day to the last day of the week will be known. Regards Vanessa
4
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
10
by: Tim Streater | last post by:
I have a form and a button to submit it. The button is made from: <input type=button onclick='myHandler(this.form);'> This all works fine except that in Safari 2.0.4, the enter/return keys, if...
1
by: peetersb | last post by:
Hi, I want ot make functions like this: int getFirtDayOfWeek(int weeknumber, int year); int getLastDayOfWeek(int weeknumber, int year); Firts I calculate the count of weeks like this: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.