vb.net 算出还有多少天

vb.net working out how many days left

这将是一个非常简单的问题(我希望),我是出于懒惰才来到这里的。

如何找出两个日期之间剩余的天数?即当前日期和 12/02/2015 之间剩余的天数?我是否需要将日期格式设置为与 "DD/MM/YYYY" 不同的格式?如果是,如何设置?

使用 CDateDatediff 函数:

Dim firstDate,secondDate As Date
Dim days as Integer
firstDate=CDate("12/02/2015")
secondDate=CDate("12/03/2017")
days=DateDiff(DateInterval.Day, firstDate, secondDate)

String 转换为 Date 时更改日期的 format 看这里Custom Date and Time Format String in C# and VB.NET:

Dim firstDate,secondDate As Date
Dim days as Integer
firstDate=CDate("12/02/2015")
secondDate=CDate("12/03/2017")
Dim numberOfDays As Integer=(secondDate-firstDate).TotalDays

很简单。您只需要使用 TimeSpan 结构的 TotalDays 属性 即可。您不需要 CDateDateDiff。示例:

Dim firstDate as New Date(2015, 12, 2)
Dim secondDate as New Date(2017, 12, 3)
Dim numberofDays as Double = (secondDate - firstDate).TotalDays