0

如何计算使用 VB.NET 完成的天数百分比?

数据读取器采用project_startproject_finished,存储Date()在 SQL-Server-2012 中。

这是我尝试过的:

Dim StartDate As New Date(datareader("project_start"))
Dim FinishDate As New Date(datareader("project_finish"))
Dim Percentage As Date = Date.FromOADate(StartDate.DayOfYear) / Date.FromOADate(FinishDate.DayOfYear) / 100

但我得到这个错误:

Operator '/' is not defined for types 'Date' and 'Date'.
4

1 回答 1

4

您需要减去 Dates 并使用 TotalDays 属性。下面的示例代码:

Dim start As DateTime = DateTime.Now.AddDays(-50)
Dim endDate As DateTime = DateTime.Now.AddDays(50)

Dim today As DateTime = DateTime.Now

Dim sumDays = (endDate - start).TotalDays
Dim daysToNow = (today - start).TotalDays

Dim percentage = daysToNow / sumDays * 100

Console.WriteLine(percentage)

Console.ReadLine()
于 2013-04-16T19:38:03.713 回答