如果我有含有格式化這樣的日期的字符串:特殊日期格式化的字符串日期(VB.net)
1402-3
這意味着年:2014,周:02和天數3(星期一是1 ),我怎樣才能將其轉換爲正常日期? (在這種情況下,上面的日期是今天; 2014-01-08 - 週三2014年1月)
編輯:我想出了這樣的功能,任何人都可以告訴如果這會失敗或可能有更好的和更好的編碼功能/解決方案
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
看看http://stackoverflow.com/questions/662379/calculate-date-from-week-number幫助。它幾乎是一個類似的問題,但在C# – saamorim
嗯,它至少是我見過的最接近的答案。謝謝。 :) – gubbfett