vb.net 如何确定某个日期是否是该月的最后一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18501706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if a date is the last day of the month?
提问by J-J
Can somebody help me on how to determine if the given date was the last date or day in a month?
有人可以帮助我确定给定的日期是一个月中的最后一天还是最后一天?
For example I have two dates, the one is 2013-01-27 and the other one is 2013-02-28. I need to determine if which date was the last day. So the one that must be display was the 2013-02-28 because its the last day of the month in february while 2013-01-27 was not the last day of the month in January.
例如我有两个日期,一个是 2013-01-27,另一个是 2013-02-28。我需要确定哪一天是最后一天。所以必须显示的是 2013-02-28,因为它是二月的最后一天,而 2013-01-27 不是一月的最后一天。
What will be the condition that I can use?
我可以使用的条件是什么?
Thanks in advance.
提前致谢。
回答by SysDragon
Use this:
用这个:
Function IsLastDay(ByVal myDate As Date) As Boolean
return myDate.Day = Date.DaysInMonth(myDate.Year, myDate.Month)
End Function
回答by Shawn de Wet
Add one day and if the resulting answer is not the same month, your date is the last day of the month.
添加一天,如果结果答案不是同一个月,则您的日期是该月的最后一天。
回答by Enigmativity
This works for me:
这对我有用:
Dim isLastDayOfMonth As Func(Of DateTime, Boolean) = _
Function (dt) dt.AddDays(1).Day = 1
回答by Vahx
Hopefully this helps you, its the code for the suggested answer of Shawn de Wet. You can switch Datetime.today.month for another variable with a specific date, then just switch the Today.Adddays(1) with the specific date.AddDays(1)
希望这对您有所帮助,这是 Shawn de Wet 建议答案的代码。您可以将 Datetime.today.month 切换为具有特定日期的另一个变量,然后只需将 Today.Adddays(1) 与特定 date.AddDays(1) 切换
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
--
——
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
回答by marcelrf
Or you can check if the following day is the first day of the month.
或者您可以检查第二天是否是该月的第一天。
If you are using ruby, you can use the condition:
如果您使用 ruby,则可以使用条件:
(date + 1).day == 1
Or if you are using python:
或者,如果您使用的是 python:
(date + datetime.timedelta(days=1)).day == 1
Cheers!
干杯!

