tags:

views:

265

answers:

3

Would anyone know how to convert any date first into a Saturday and then into yyww (weekyear) in VB.NET? And vice versa, ie yyww into a Saturday?

A: 

You mean find the Saturday for any given week?

Damien
I first need to convert a date into the Saturday of that week, then convert the Saturday into yyww. Otherwise, just converting a date into yyww will not return consistent results.
Greg
+2  A: 

Assuming your weeks start on Sunday, you could probably do something like this to get the saturday of the current week:

Dim daysToAdd As Integer = DayOfWeek.Saturday - yourDate.DayOfWeek
Dim dateSaturday as Date = yourDate.AddDays(daysToAdd)

I didn't test it though. Then you could get the weekyear by doing something like:

Dim weekYear As Integer = CInt(Math.Ceiling(dateSaturday.DayOfYear / 7))

Using the reverse logic you can find the saturday corresponding to the weekyear.

Meta-Knight
+2  A: 
    Dim d As DateTime = #1/20/2009#
    d = d.AddDays(DayOfWeek.Saturday - d.DayOfWeek)
    Dim myCal As New System.Globalization.GregorianCalendar
    Dim wkOFyr As Integer = myCal.GetWeekOfYear(d, _
                                                Globalization.CalendarWeekRule.FirstDay, _
                                                DayOfWeek.Sunday)
    Dim s As String
    s = d.ToString("yy") & wkOFyr.ToString.PadLeft(2, "0"c)
dbasnett