我希望检查当前日期是否是该月的第一个工作日,我想知道是否有更简单的方法可以做到这一点?目前,我使用以下编码来确定第一个工作日,然后将其与另一个具有今天日期的文本框进行匹配。
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Dte As Date = FirstMonday(1, 2015)
Me.TextBox1.Text = Dte.ToString
End Sub
Private Function FirstMonday(ByVal Mnth As Integer, ByVal year As Integer) As Date
Dim Dte As Date = New Date(year, Mnth, 1)
Do While Dte.DayOfWeek <> DayOfWeek.Monday
Dte = Dte.AddDays(1)
Loop
Return Dte
End Function
答案 0 :(得分:2)
这里有一个函数可以返回月份的第一个星期一,另一个函数可以验证给定的日期是否是这个星期一,最后是一个函数来执行此验证但是今天。
private static DateTime FirstMonday(int month, int year)
{
DateTime date = new DateTime(year, month, 1);
int daysToAdd = 8 - (int)date.DayOfWeek;
if (daysToAdd >= 7)
daysToAdd -= 7;
return date.AddDays(daysToAdd);
}
private static bool IsFirstMondayOfMonth(DateTime date)
{
if (date.DayOfWeek != DayOfWeek.Monday || date.Day > 7)
return false;
return true;
}
private static bool IsTodayFirstMondayOfMonth()
{
if (DateTime.Today.DayOfWeek != DayOfWeek.Monday || DateTime.Today.Day > 7)
return false;
return true;
}
他们在C#中,但我相信你可以很容易地将它们转换为VB。
最后,关于评论中的请求,您只需使用date.ToShortDateString()
获取dd / MM / yyyy格式,如果这是您配置的文化。如果您想确保在所有情况下都使用该格式,请改用date.ToString("dd/MM/yyyy")
。
答案 1 :(得分:0)
我希望检查当前日期是否是该月的第一个工作日
很好,但第一个周一与此有什么关系?如果月份从星期五开始怎么办?还是星期二?然后第一个星期一是错误的结果。
我发现最简单的方法是从月初开始,直到你找到一个工作日为止:
Private Shared Function FirstWeekDayOfCurrentMonth() As DateTime
'we check DateTime.Today twice in this function
'so save first result to avoid race condition near midnight
Dim current As DateTime = Date.Today
current = current.AddDays(-1 * (current.Day - 1) )
While current.DayOfWeek = DayOfWeek.Sunday OrElse current.DayOfWeek = DayOfWeek.Saturday
current = current.AddDays(1)
End While
Return current
End Function
我知道有时候需要为这样的事情编写一个循环感觉效率低下,但最多你会对这段代码进行两次迭代,所以它仍然非常好。然后检查变得微不足道了:
Private Shared Function IsFirstWeekDayOfCurrentMonth() As Boolean
Return DateTime.Today.Equals( FirstWeekDayOfCurrentMonth() )
End Function
请注意,在第一个函数中,我保存了DateTime.Today
值的副本,以避免在系统时钟翻转时午夜运行时出现奇怪结果的可能性。实际上,你应该跨功能这样做,所以一个更好的程序设计可以这样工作:
Private Shared Function FirstWeekDayOfMonth(DateTime month) As DateTime
'remove any time component and set to first day of the month
month = month.Date.AddDays(-1 * (current.Day - 1) )
While month.DayOfWeek = DayOfWeek.Sunday OrElse month.DayOfWeek = DayOfWeek.Saturday
month = month.AddDays(1)
End While
Return month
End Function
Private Shared Function IsFirstWeekDayOfMonth(DateTime dateToCheck ) As Boolean
Return dateToCheck.Date.Equals( FirstWeekDayOfMonth(dateToCheck) )
End Function
然后你会这样称呼它:
If IsFirstWeekDayOfMonth(DateTime.Today) Then
'...
End IF
答案 2 :(得分:0)
你可以尝试这个功能
Public Function FirstMonday(year As Integer, month As Integer) As DateTime
Dim firstDay As New DateTime(year, month, 1)
If firstDay.DayOfWeek = DayOfWeek.Monday Then
Return firstDay
Else
Dim days = DayOfWeek.Monday - firstDay.DayOfWeek ' in case the first day was less than monday
If firstDay.DayOfWeek > DayOfWeek.Monday Then days = 7 - firstDay.DayOfWeek + DayOfWeek.Monday ' in case if the first day was > monday
Return firstDay.AddDays(days)
End If
End Function
使用此功能
dim monday=FirstMonday(2014,6) ' for example
如果您想要将函数概括为可以使用任何DayOfWeek,您可以使用以下函数
Public Function FirstDayOfWeek(year As Integer, month As Integer, Day As DayOfWeek) As DateTime
Dim firstDay As New DateTime(year, month, 1)
If firstDay.DayOfWeek = Day Then
Return firstDay
Else
Dim days = Day - firstDay.DayOfWeek ' in case the first day was less than DayOfWeek
If firstDay.DayOfWeek > Day Then days = 7 - firstDay.DayOfWeek + Day ' in case if the first day was > DayOfWeek
Return firstDay.AddDays(days)
End If
End Function
这是一个有效的DEMO
演示的结果是:哪个准确
First Monday in [Jan 2014] is 06 Jan 2014
First Monday in [Feb 2014] is 03 Feb 2014
First Monday in [Mar 2014] is 03 Mar 2014
First Monday in [Apr 2014] is 07 Apr 2014
First Monday in [May 2014] is 05 May 2014
First Monday in [Jun 2014] is 02 Jun 2014
First Monday in [Jul 2014] is 07 Jul 2014
First Monday in [Aug 2014] is 04 Aug 2014
First Monday in [Sep 2014] is 01 Sep 2014
First Monday in [Oct 2014] is 06 Oct 2014
First Monday in [Nov 2014] is 03 Nov 2014
First Monday in [Dec 2014] is 01 Dec 2014