2012-10-20 89 views
5

今天是21.10.2012。现在是星期天。
但我的VB.NET想的不一样:Vb.net获取周日名称

Debug.Print("Weekday for the date '21.10.2012.'is " & WeekdayName(Weekday("21.10.2012."))) 
Debug.Print("Weekday for the date '21/10/2012'is " & WeekdayName(Weekday("21/10/2012"))) 
Debug.Print("Weekday for the date '" & DateTime.Now.Date & "'is " & WeekdayName(Weekday(DateTime.Now.Date))) 

所有这些3张支票给我: '星期一' 平日的名字!
如何获得正确的周日名称?

+0

我知道这是老了,但想到我会解释为什么这是行不通的。 Weekday返回一个表示星期几的整数,从1 =星期日开始。 WeekdayName函数以周一= 1开始。这就是为什么它不同步并返回错误的日期名称。 – Aki

回答

8

这是正确的.NET的方式来获得一个给定日期的星期名称:

Dim myCulture As System.Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture 
Dim dayOfWeek As DayOfWeek = myCulture.Calendar.GetDayOfWeek(Date.Today) 
' dayOfWeek.ToString() would return "Sunday" but it's an enum value, 
' the correct dayname can be retrieved via DateTimeFormat. 
' Following returns "Sonntag" for me since i'm in germany ' 
Dim dayName As String = myCulture.DateTimeFormat.GetDayName(dayOfWeek) 
+0

谢谢蒂姆,这个作品。在我的问题,我也获得当地的名字,所以我现在想知道在哪个文化21.10.2012。是星期一? :) –

+0

@ user973238:我不确定它是否得到,也许你应该问一个真正的问题,这听起来很有趣。 –

0
Label1.Text = Date.Today.ToString("dddd") 
+1

这并没有解决提问者的实际问题。 –