以数字格式获取当天最有效的方法?

时间:2015-08-06 07:20:54

标签: vb.net performance dayofweek

我有这段代码来获取当天(例如星期一,星期二)并将其转换为数字格式,从SELECT TERMID, PAGE_VIEWS, SUM(PAGE_VIEWS) OVER () AS TOTAL_PAGE_VIEWS FROM ( SELECT aip.TERMID, COUNT(rau.TERMNAME) AS PAGE_VIEWS FROM REPORT_API_PAGES aip LEFT OUTER JOIN REPORT_API_USAGE rau ON (rau.TERMID = aip.TERMID AND rau.VOCID = 4) GROUP BY aip.TERMID) saturday = 1等开始。

monday = 2

有没有一种有效或更好的方法来做到这一点?也许是内置的vb.net功能?

3 个答案:

答案 0 :(得分:3)

您可以对此类情况使用DayOfWeek枚举。

  Dim dayNumber As Integer = Now.DayOfWeek()

答案 1 :(得分:1)

由于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()))

Fiddle Demo

答案 2 :(得分:0)

您可以使用

int dayInNumber = (int)Now.DayOfWeek

将当天作为一个数字。如果是星期天,您可能想要做类似

的事情
 if(dayInNumber == 0){ dayInNumber = 7;}

因为枚举从0开始

代码是C#语法,但您可以将其转换为vb.net中的等效语法