关于vb.net:如何确定日期是否为该月的最后一天?

How to determine if a date is the last day of the month?

有人可以帮我确定给定的日期是一个月的最后日期还是一天?

例如,我有两个日期,一个是2013-01-27,另一个是2013-02-28。 我需要确定哪个日期是最后一天。 因此,必须显示的是2013-02-28,因为它是2月的月份的最后一天,而2013-01-27并不是1月的月份的最后一天。

我可以使用的条件是什么?

提前致谢。


用这个:

1
2
3
Function IsLastDay(ByVal myDate As Date) As Boolean
    return myDate.Day = Date.DaysInMonth(myDate.Year, myDate.Month)
End Function


添加一天,如果结果答案不是同一个月,则您的日期为该月的最后一天。


这对我有用:

1
2
Dim isLastDayOfMonth As Func(Of DateTime, Boolean) = _
    Function (dt) dt.AddDays(1).Day = 1

希望这对您有所帮助,它是Shawn de Wet建议答案的代码。
您可以将Datetime.today.month切换为具有特定日期的另一个变量,然后只需将Today.Adddays(1)切换为特定日期。AddDays(1)

1
2
3
4
5
6
7
8
9
10
Dim dateOfToday As DateTime = Today.AddDays(1)

Sub isTodayTheLastDay()
    If DateTime.Today.Month = dateOfToday.Month Then
        'yes its the same month, place code here
    Else
        'no its not the same month, place code here
    End If

End Sub

-

1
2
3
4
5
6
7
8
9
10
11
12
Dim dateToCheck As New DateTime(2013, 4, 21) 'any date you want to check here
Dim Check As DateTime = dateToCheck.AddDays(1)


Sub isTodayTheLastDay()
    If dateToCheck.Month = Check.Month Then
        'yes its the same month, place code here
    Else
        'no its not the same month, place code here
    End If

End Sub


或者,您可以检查第二天是否是该月的第一天。

如果您使用的是红宝石,则可以使用以下条件:

1
(date + 1).day == 1

或者,如果您使用的是python:

1
(date + datetime.timedelta(days=1)).day == 1

干杯!