DateTime question in VB.NET
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 when we roll over to next week it needs to calculate: 16/11/2009
I have tried doing this myself but I can开发者_StackOverflow't see how to do the arithmetic, thank you.
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))
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.
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
.
精彩评论