|
-
Apr 14th, 2007, 04:57 AM
#1
Thread Starter
Hyperactive Member
[2005] Date problem Pls help
I am working on an app where a date is selected to pull data and I have to display the data along with dates and week number of the year that the date full in. So I would be grateful if any of you could help me on either how I could store the week number, so that I can pull it or to display the week number when the date is selected. Example today is 14/04/2007 which full within week 15 of year 2007.
Thanks.
-
Apr 14th, 2007, 05:07 AM
#2
Re: [2005] Date problem Pls help
It's not very hard to calculate the week of the year from the date, making use of the Date.DayOfYear property and the fact that there are 7 days in a week. You can also use the Date.DayOfWeek property to determine what day of the week the first of January was to correct the number of days in the first week.
-
Apr 14th, 2007, 06:51 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Date problem Pls help
 Originally Posted by jmcilhinney
It's not very hard to calculate the week of the year from the date, making use of the Date.DayOfYear property and the fact that there are 7 days in a week. You can also use the Date.DayOfWeek property to determine what day of the week the first of January was to correct the number of days in the first week.
Thanks Jmcilhinney, but what I would like to return is the actual week number of the year, as one year has 52 weeks today is among the 15th week so if you select today's date it should return 15. I try something like this but some days return the right week of the year while others days does not return the actual week of the year.
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Dim sWeekOfYear As Integer = dtp1.Value.DayOfYear
Dim sWeek As Integer = (sWeekOfYear / 7)
Me.lbl2.Text = "Week " & sWeek.ToString
Me.lbl1.Text = dtp1.Value.DayOfWeek.ToString
End Sub
End Class
-
Apr 14th, 2007, 07:04 AM
#4
Re: [2005] Date problem Pls help
That will return the correct value assuming that the first week of the year starts on the first day of the year. How do you define the first week of the year?
-
Apr 14th, 2007, 07:35 AM
#5
Thread Starter
Hyperactive Member
Re: [2005] Date problem Pls help
 Originally Posted by jmcilhinney
That will return the correct value assuming that the first week of the year starts on the first day of the year. How do you define the first week of the year?
First week is week 1. Jmcilh if you look at monthcalender 30/04/2007 is in week 18 but mine return week 17. I have try if I can use monthcalender control but still not able to get the week number wright. any help thanks.
-
Apr 14th, 2007, 08:33 AM
#6
Re: [2005] Date problem Pls help
 Originally Posted by wiadus
First week is week 1.
I never would have guessed. Take a look at a MonthCalendar for January of different years. Assuming that your regional settings are the same as mine, you'll see that the first week of the year is the first Monday to Sunday period that contains at least 4 days of the new year. For the last several years that looks like this:
2007: Jan 1 to Jan 7
2006: Jan 2 to Jan 8
2005: Jan 3 to Jan 9
2004: Dec 29 to Jan 4
So you see, the first week of the year is NOT defined simply as the first 7 days of the year, or at least not by most standard regional settings.
You can determine how the first week of the year is calculated by the Globalization.CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule property. You can determine what day of the week the first day of the current year was by:
VB Code:
New Date(Date.Today.Year, 1, 1).DayOfWeek
Put those together and you can work out the date of the first day week 1. Then you can calculate the number of days since that date and divide by 7 to get the week number.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|