2011-01-20 164 views
1

是否有一個簡單的內置函數,可以用來從日曆/年度組合中獲取日期實例?將日曆轉換爲日期

應該可以在文本框中輸入10w2005,我將從中創建日期07 March 2005

星期一應該是第一天,CalendarWeekRule應該是FirstFullWeek。

我的第一種方法是:

Dim w As Int32 = 10 
Dim y As Int32 = 2005 
Dim d As New Date(y, 1, 1) 
d = d.AddDays(7 * w) 

但由於沒有應用FirstDay- CalendarWeekRule這不起作用。

在此先感謝

編輯

這是我和(感謝fjdumont的鏈接)結束:

Public Shared Function FirstDateOfWeek(ByVal year As Integer, ByVal weekOfYear As Integer) As DateTime 
     Dim jan1 As New DateTime(year, 1, 1) 
     Dim daysOffset As Integer = CInt(Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) - CInt(jan1.DayOfWeek) 
     Dim firstWeekDay As DateTime = jan1.AddDays(daysOffset) 
     Dim curCulture As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture 
     Dim firstWeek As Integer = curCulture.Calendar.GetWeekOfYear(jan1, curCulture.DateTimeFormat.CalendarWeekRule, curCulture.DateTimeFormat.FirstDayOfWeek) 
     If firstWeek <= 1 Then 
      weekOfYear -= 1 
     End If 
     Return firstWeekDay.AddDays(weekOfYear * 7) 
End Function 

回答

1

This thread應該有所幫助。你將不得不解析字符串,有點像這樣。

string[] spl = input.ToLower().Split("w"); 
int year = int.Parse(spl[1]); 
int week = int.Parse(spl[0]); 
+0

所以答案是:有沒有簡單的內置函數。謝謝。 – 2011-01-20 13:01:16

0
Dim dfi As Globalization.DateTimeFormatInfo = Globalization.DateTimeFormatInfo.CurrentInfo 
    Dim cal As Globalization.Calendar = dfi.Calendar 

    '10w2005 
    Dim w As Integer = 10 'week 
    Dim y As Integer = 2005 
    Dim d As DateTime = New DateTime(y, 1, 1) 

    If cal.GetWeekOfYear(d, Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday) <> 1 Then 
     w -= 1 
     d = d.AddDays(8 - d.DayOfWeek) 
    End If 

    d = d.AddDays(7 * w) 
    Debug.WriteLine(d.ToString) 
+0

我已將我的解決方案添加到了我的問題中。不管怎麼說,還是要謝謝你。 – 2011-01-21 14:22:03