DateTime question in VB.NET

前端 未结 3 1592
孤街浪徒
孤街浪徒 2020-12-21 18:09

Ok, so I need to find the date of Monday this week programmatically.

For example, for this week Monday was on the 9th, so the date I need is: 09/11/2009

And

相关标签:
3条回答
  • 2020-12-21 18:36
    Return givenDate.AddDays(1 - CType(IIf((givenDate.DayOfWeek = DayOfWeek.Sunday), 7, givenDate.DayOfWeek), Double))
    

    If givenDate is a Sunday, counts back to the preceding Monday. Includes a CType to cast the IIf result to a Double to work with Option Strict On.

    0 讨论(0)
  • 2020-12-21 18:53
    Dim thisMonday As Date = Now.AddDays((Now.DayOfWeek - 1) * -1).Date
    

    If today is a Sunday it gives the following Monday otherwise, gives the Monday this week.

    0 讨论(0)
  • 2020-12-21 18:55

    C#:

    date.AddDays(1 - (date.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)date.DayOfWeek));
    

    VB.NET:

    date.AddDays(1 - IIf((date.DayOfWeek = DayOfWeek.Sunday), 7, date.DayOfWeek))
    
    0 讨论(0)
提交回复
热议问题