0

If i have a string containing a date formatted like this:

1402-3

which means Year: 2014, Week: 02 and Day number 3 (monday is 1), how can i convert this to a normal date? (in this case the date above is today; 2014-01-08 - wednesday 8 jan 2014)

Edit: I came up with a function like this, can anyone tell if this is gonna fail or maybe have a better and better coded function/solution?

Private Function StrangeFormattedDateToRegularDate(ByVal StrangeDate As String) As Date
    Dim Y As String = "20" & StrangeDate.Substring(0, 2) 'I'll be dead before this fails, haters gonna hate
    Dim W As String = StrangeDate.Substring(2, 2)
    Dim D As String = StrangeDate.Substring(5, 1)

    'Get first day of this year
    Dim RefDate As Date = New Date(CInt(Y), 1, 1)

    'Get the first day of this week (can be the year before)
    Dim daysOffSet As Integer = DayOfWeek.Monday - RefDate.DayOfWeek
    RefDate = RefDate.AddDays(daysOffSet)

    'Add as many days as the weeks is
    RefDate = RefDate.AddDays(7 * CInt(W))

    'now the date is the last day of this week (plus one day), remove the days that are ahead, and remove that extra day
    Dim daysToRemove = ((7 - CInt(D)) * -1) - 1
    RefDate = RefDate.AddDays(daysToRemove)

    Return RefDate
End Function
gubbfett
  • 2,157
  • 6
  • 32
  • 54

1 Answers1

1

This should be what you're looking for :) This looked challenging so I tried it. Tell me if it works for you or not :)

 Function GetDate(InputDate As String) As DateTime
    Dim FirstDayofYear As Date = CType("1/1/20" & Mid(InputDate, 1, 2), Date)
    Dim LastDayofYear As Date = CType("12/31/20" & Mid(InputDate, 1, 2), Date)
    Dim target As Date
    For x = 0 To DateDiff(DateInterval.Day, FirstDayofYear, LastDayofYear)
        Dim dfi = DateTimeFormatInfo.CurrentInfo
        Dim calendar = dfi.Calendar
        Dim weekOfyear = calendar.GetWeekOfYear(FirstDayofYear.AddDays(x), dfi.CalendarWeekRule, DayOfWeek.Sunday)
        If CInt(Mid(InputDate, 3, 2)) = weekOfyear And CInt(Mid(InputDate, InStr(InputDate, "-") + 1)) = FirstDayofYear.AddDays(x).DayOfWeek Then
            target = FirstDayofYear.AddDays(x)
            GoTo skip
        End If
    Next x
skip:
    Return target
End Function

This works up to Year 2099. We're probably all dead by then.

chris_techno25
  • 2,401
  • 5
  • 20
  • 32
  • Cool solution, i'll give it a try tomorrow when i'm back at that computer. What i can tell of this solution, you get the first day of the year and loops every day until last day for the year, and for every day you check "is this week 02 and day number 3?" if yes, return this date or if false, go to next date? I'm not sure, but this solution feels a bit slower than my solution above? i like your way of thinking tho, and your solution feels more fail proof. My example worked for some random dates i tried with, but i am not sure it will work perfect for all dates :) – gubbfett Jan 08 '14 at 15:52
  • Yup my friend, it should be slower technically coz the code loops a maximum of 365. But I can't really tell if it's really that slow because I don't have anything to compare to. I made the algo myself coz I couldn't find anything close to it. But looping through 365 is really fast that, well in my case, I don't see any lag. My solution may be more fail proof, but I don't know, whichever you're more comfortable with. Anybody can provide a better solution than mine but that's my best for now. Just wanted to test myself and help others :) – chris_techno25 Jan 08 '14 at 15:58
  • And i appreciate your solution. Really! I'll give it a try tomorrow. I guess there's hundreds of ways to do this, and i'll keep the thread open for some days if anyone comes up with the one row solution i hope for. :) Cheers! – gubbfett Jan 08 '14 at 16:18