老师,请问如何根据给定的年份和周别返回这一周的日期,代码怎么写?如:给定2023年第36周,如何写代码得到这一周的第一天或最后一天?
Dim n As Integer = 36 '周次
For i As Integer = 1 To DatePart("ww", New Date(2024, 12, 31))
Dim d As Date = New Date(2024, 1, 1)
d = d.AddDays(7 * (i - 1))
If i = n Then
Output.Show(cexp("第{0}周的第1天{1},最后一天{2}", n, d, d.AddDays(6)))
End If
Next
运行结果:
第36周的第1天2024-09-02,最后一天2024-09-08
感谢回答,但这个好象不对吧,你这是从1月1日开始按7天相乘得到的结果,不准确
Dim d As Date = Date.Today
Output.Show(d.DayOfWeek),通过当天是星期几反算吧
Dim year As Integer = 2024
Dim week As Integer = 1
Dim d As Date = New Date(year, 1, 1)
Dim w = d.DayOfWeek
Dim startdate As Date
Dim Enddate As Date
Select Case week
Case 0
startdate = d
Enddate = d.AddDays(7 - w - 1)
Case 53
startdate = d.AddDays(7 - w).AddDays(7 * 51)
Enddate = d.AddYears(1).AddDays( - 1)
Case Else
startdate = d.AddDays(7 - w).AddDays(7 * (week - 2))
Enddate = startdate.AddDays(6)
End Select
Output.Show(startdate & "-" & Enddate)
[此贴子已经被作者于2024/5/30 9:18:49编辑过]
怎样写成带year和week两个参数的自定义函数?
Dim year As Integer = args(0)
Dim week As Integer = args(1)
谢谢老师
[此贴子已经被作者于2024/5/30 14:36:45编辑过]