以数字格式获取当前日期的最有效方法?
Most efficient way to get the current day in number format?
我有这段代码可以获取当前日期(例如星期一、星期二)并将其转换为数字格式,以 saturday = 1
、monday = 2
等开头
Dim Current_Day As String = Format(Now, "dddd")
Dim Day_Name_Val As String = "0"
Select Case Current_Day
Case "Saturday"
Day_Name_Val = "1"
Case "Monday"
Day_Name_Val = "2"
Case "Tuesday"
Day_Name_Val = "3"
Case "Wednesday"
Day_Name_Val = "4"
Case "Thursday"
Day_Name_Val = "5"
Case "Friday"
Day_Name_Val = "6"
Case "Sunday"
Day_Name_Val = "7"
End Select
是否有有效或更好的方法来做到这一点?也许是 vb.net?
的内置函数
您可以使用
int dayInNumber = (int)Now.DayOfWeek
以数字形式获取当前日期。如果是星期天,你可能想做类似
的事情
if(dayInNumber == 0){ dayInNumber = 7;}
因为枚举是从0开始的
代码采用 C# 语法,但您可以将其转换为 vb.net
中的等效语法
对于这种情况,您可以使用 DayOfWeek 枚举。
Dim dayNumber As Integer = Now.DayOfWeek()
因为 DayOfWeek
的默认值是:
Enum DayOfWeek
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
End Enum
我会定义你自己的:
Enum MyDayOfWeek
Saturday = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Sunday = 7
End Enum
这样你就可以做类似的事情:
Dim Day_Name_Val = Convert.ToInt32([Enum].Parse(GetType(MyDayOfWeek),
DateTime.Now.DayOfWeek.ToString()))
我有这段代码可以获取当前日期(例如星期一、星期二)并将其转换为数字格式,以 saturday = 1
、monday = 2
等开头
Dim Current_Day As String = Format(Now, "dddd")
Dim Day_Name_Val As String = "0"
Select Case Current_Day
Case "Saturday"
Day_Name_Val = "1"
Case "Monday"
Day_Name_Val = "2"
Case "Tuesday"
Day_Name_Val = "3"
Case "Wednesday"
Day_Name_Val = "4"
Case "Thursday"
Day_Name_Val = "5"
Case "Friday"
Day_Name_Val = "6"
Case "Sunday"
Day_Name_Val = "7"
End Select
是否有有效或更好的方法来做到这一点?也许是 vb.net?
的内置函数您可以使用
int dayInNumber = (int)Now.DayOfWeek
以数字形式获取当前日期。如果是星期天,你可能想做类似
的事情 if(dayInNumber == 0){ dayInNumber = 7;}
因为枚举是从0开始的
代码采用 C# 语法,但您可以将其转换为 vb.net
中的等效语法对于这种情况,您可以使用 DayOfWeek 枚举。
Dim dayNumber As Integer = Now.DayOfWeek()
因为 DayOfWeek
的默认值是:
Enum DayOfWeek
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
End Enum
我会定义你自己的:
Enum MyDayOfWeek
Saturday = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Sunday = 7
End Enum
这样你就可以做类似的事情:
Dim Day_Name_Val = Convert.ToInt32([Enum].Parse(GetType(MyDayOfWeek),
DateTime.Now.DayOfWeek.ToString()))