2016-03-30 166 views
-6

請有人提供上週第一天和最後一天的代碼?獲取前一週的第一天和最後一天

在此先感謝。

+4

[一些常見的日期程序(http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25 /一些常見最新例程/)。 –

+0

什麼*是*本週的第一天?這是文化的依賴。 –

回答

1

您可以使用這些功能:

Public Function DateWeekFirst(_ 
    ByVal datDate As Date, _ 
    Optional ByVal lngFirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) _ 
    As Date 

' Returns the first date of the week of datDate. 
' lngFirstDayOfWeek defines the first weekday of the week. 
' 2000-09-07. Cactus Data ApS. 
' 2003-05-01. System settings used as default. 
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek. 

    DateWeekFirst = DateAdd("d", vbSunday - Weekday(datDate, lngFirstDayOfWeek), datDate) 

End Function 

Public Function DateWeekLast(_ 
    ByVal datDate As Date, _ 
    Optional ByVal lngFirstDayOfWeek As Long = vbUseSystemDayOfWeek) _ 
    As Date 

' Returns the last date of the week of datDate. 
' lngFirstDayOfWeek defines the first weekday of the week. 
' 2000-09-07. Cactus Data ApS. 
' 2003-05-01. System settings used as default. 
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek. 

    DateWeekLast = DateAdd("d", vbSaturday - Weekday(datDate, lngFirstDayOfWeek), datDate) 

End Function 

然後,例如:

FirstDatePreviousWeek = DateWeekFirst(DateAdd("ww", -1, Date)) 
相關問題