如何计算使用VB.NET完成的天数百分比?
datareader采用project_start
和project_finished
,在SQL-Server-2012中存储为Date()
。
这就是我的尝试:
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'.
答案 0 :(得分:4)
您需要减去日期并使用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()