如何获取当周的开始日期和结束日期?

时间:2016-06-19 21:50:44

标签: vb.net

由于本论坛的一个答案,我有一个函数可以返回我们现在的当前一周。现在我需要获得该周的初始日期和该周的最终日期。

我该如何做到这一点?

Protected Friend Function obtenerNumeroSemanas(ByVal dt As DateTime)
    Dim cal As Calendar = CultureInfo.InvariantCulture.Calendar
    Dim d As DayOfWeek = cal.GetDayOfWeek(dt)
    If (d >= DayOfWeek.Monday) AndAlso (d <= DayOfWeek.Wednesday) Then
        dt = dt.AddDays(3)
    End If
    Return cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
End Function

2 个答案:

答案 0 :(得分:4)

Flux

使用今天的日期(2016-06-19)结果

Dim dayOfWeek = Cint(DateTime.Today.DayOfWeek)
Dim startOfWeek = DateTime.Today.AddDays(-1 * dayOfWeek)
Dim endOfWeek = DateTime.Today.AddDays(7 - dayOfWeek).AddSeconds(-1)
Console.WriteLine(startOfWeek)
Console.WriteLine(endOfWeek)

答案 1 :(得分:0)

Dim dayOfWeek = CInt(DateTime.Today.DayOfWeek)
Dim startOfWeek = DateTime.Today.AddDays(+1 * dayOfWeek).ToShortDateString
Dim endOfWeek = DateTime.Today.AddDays(4 + dayOfWeek).AddSeconds(+1).ToShortDateString
MessageBox.Show(startOfWeek)
MessageBox.Show(endOfWeek)

获取下一个星期一日期和下一个星期五