如何将周数转换为该周的开始日期?示例:2016年第41周至10月9日?日期还必须取决于年份。示例:对于2017年,第41周必须等于10月8日。
我已经搜索过并找到了这个(How to get starting date in a Week based on week number using vb.net?),但这是针对vb.net的。我不确定这是否适用于vb6。如果这在vb6中不起作用,我该怎么办?
谢谢:)
答案 0 :(得分:3)
试试这个:
Private Sub Form_Load()
Dim d As Date
d = GetWeekStartDate(41, 2016)
MsgBox d
d = GetWeekStartDate(41, 2017)
MsgBox d
End Sub
Private Function GetWeekStartDate(weekNumber As Integer, year As Integer) As Date
Dim startDate As Date
Dim day As Integer
startDate = DateSerial(year, 1, 1)
day = Weekday(startDate, vbSunday)
startDate = DateAdd("d", DaysToAdd(day), startDate)
GetWeekStartDate = DateAdd("ww", weekNumber - 1, startDate)
End Function
Private Function DaysToAdd(day As Integer) As Integer
DaysToAdd = 0
If day > 1 Then DaysToAdd = 7 - day + 1
End Function
答案 1 :(得分:1)
如果您不需要所有选项,可以简化此操作,但它看起来是正确的:
Option Explicit
Private Function WeekDate( _
ByVal Year As Long, _
ByVal Week As Long, _
Optional ByVal FirstWholeWeekIs1 As Boolean, _
Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) As Date
Dim YearDate As Date
Dim WeekdayOffset As Long
Dim WeekOffset As Long
YearDate = DateSerial(Year, 1, 1)
WeekdayOffset = Weekday(YearDate, FirstDayOfWeek) - 1
If WeekdayOffset <> 0 Then
YearDate = DateAdd("d", -WeekdayOffset, YearDate)
WeekOffset = IIf(FirstWholeWeekIs1, 0, 1)
Else
WeekOffset = 1
End If
WeekDate = DateAdd("ww", Week - WeekOffset, YearDate)
End Function
Private Sub Form_Load()
Dim Year As Long
Dim Week As Long
AutoRedraw = True
Year = 2015
Week = 1
Print Year, Week, WeekDate(Year, Week)
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
Week = 2
Print Year, Week, WeekDate(Year, Week)
Print Year, Week, WeekDate(Year, Week, FirstDayOfWeek:=vbMonday)
Year = 2016
Week = 41
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
Year = 2017
Week = 1
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=False)
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
End Sub
如果不对,我确定你可以做出调整。