vb.net 计算下个月的最后一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37805092/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:56:38  来源:igfitidea点击:

Calculate the last day of the next month

vb.netdate

提问by Kongkon Mahanta

How to calculate the last day of the next monthusing vb.net?

如何使用vb.net计算下个月最后一天

回答by Fadil Zitawi

try yo use DaysInMonthFunction which returns the number of days in the required month.

尝试使用DaysInMonth函数返回所需月份的天数。

Example

例子

System.DateTime.DaysInMonth(2016,May)

This will return (31), so you can get the date format in addition to this number to get last day of this month.

这将返回 (31),因此您可以获得除此数字之外的日期格式以获取本月的最后一天。

回答by the_lotus

An other way is to get the first day of the month after and substract 1 day.

另一种方法是获取后一个月的第一天并减去 1 天。

    Dim d As DateTime

    d = DateTime.Now.AddMonths(2)
    d = New DateTime(d.Year, d.Month, 1).AddDays(-1)

回答by OSKM

If you after the actual date....

如果您在实际日期之后......

You could use DateSerial(Year, Month, 0)this will return the last day of the month prior to the month entered.

您可以使用DateSerial(Year, Month, 0)它返回输入月份之前的月份的最后一天。

Entering DateSerial(2016, 07, 0)will return "30/06/2016"

输入DateSerial(2016, 07, 0)将返回“30/06/2016”

To get the last day of next month do DateSerial(Now.Year, Now.Month + 2, 0)

要获得下个月的最后一天,请执行 DateSerial(Now.Year, Now.Month + 2, 0)

This works fine for February and also the year end as well (DateSerial(2016, 3, 0)returns "29/02/2016", DateSerial(2016, 11 + 2, 0)returns "31/12/2016")

这适用于二月和年末(DateSerial(2016, 3, 0)返回“29/02/2016”,DateSerial(2016, 11 + 2, 0)返回“31/12/2016”)

回答by dotNET

Dim N = DateTime.Now.AddMonths(1)
Dim D = New DateTime(N.Year, N.Month, DateTime.DaysInMonth(N.Year, N.Month))

Dwill have the last day of next month.

D将是下个月的最后一天。