vb.net 将 DayOfWeek 枚举转换为表示日期的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2169130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting DayOfWeek enum to a string repesenting the day
提问by James
I was wondering if there was a way to directly convert the integer DayOfWeek
returns into a string representing the day like Monday, Tuesday etc.
我想知道是否有一种方法可以将整数DayOfWeek
返回值直接转换为表示星期一、星期二等日期的字符串。
Sample code:
示例代码:
MessageBox.Show(Date.Today.DayOfWeek)
This will return 6
(as of today). Is there a way to directly convert this into Saturday
, for example? I don't really care what it really converts it into, but I want to do away with my Select Case:
这将返回6
(截至今天)。Saturday
例如,有没有办法直接将其转换为 ?我真的不在乎它真正将它转换成什么,但我想取消我的 Select Case:
Select Case Date.Today.DayOfWeek
Case 0
day = "Sunday"
Case 1
day = "Monday"
Case 2
day = "Tuesday"
Case 3
day = "Wednesday"
Case 4
day = "Thursday"
Case 5
day = "Friday"
Case 6
day = "Saturday"
Case Else
day = "Apocalypse: we're all boned."
End Select
Thanks :)
谢谢 :)
采纳答案by Rubens Farias
A simpler way:
一个更简单的方法:
Dim day As String = Date.Today.DayOfWeek.ToString()
回答by itowlson
DateTimeFormatInfo.CurrentInfo.GetDayName.
日期时间格式信息.当前信息。获取日名。
回答by Zyphrax
There's a DateTime format for that: dddd
有一个 DateTime 格式:dddd
Dim date1 As Date = #08/29/2008 7:27:15PM#
date1.ToString("dddd", CultureInfo.CreateSpecificCulture("en-US"))
With the CultureInfo you can get it in a specific language (it's optional)
使用 CultureInfo,您可以获得特定语言的信息(可选)
For more info: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ddddSpecifier
更多信息:http: //msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ddddSpecifier
回答by Jon Skeet
DateTime.DayOfWeek
doesn't return an integer - it returns an enum of type DayOfWeek
. I'd expect that to be converted into the name automatically, but maybe this is a VB subtlety; maybe something to do with using Date
instead of DateTime
? Try this:
DateTime.DayOfWeek
不返回整数 - 它返回类型的枚举DayOfWeek
。我希望它会自动转换为名称,但也许这是 VB 的微妙之处;也许与使用Date
而不是有关DateTime
?尝试这个:
MessageBox.Show(DateTime.Today.DayOfWeek.ToString())
This won't be culture-sensitive though - it will always just display the name of enum value, in English. If that's not good for you, use Zyphrax or itowlson's solution.
但这不会是文化敏感的 - 它总是只显示枚举值的名称,英文。如果这对您不利,请使用 Zyphrax 或 itowlson 的解决方案。
回答by CoderDennis
Date.Today.DayOfWeek.ToString
will give you what you're looking for. Nice and easy.
Date.Today.DayOfWeek.ToString
会给你你正在寻找的东西。好,易于。
回答by Greg Gage
Just in case others looked at this example. I believe it should be Case 0 for Sunday.
以防万一其他人看到这个例子。我相信周日应该是 Case 0。
Case 0
day = "Sunday"