计算VB.NET的天数百分比

时间:2013-04-16 19:30:47

标签: asp.net vb.net date sql-server-2012 percentage

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

datareader采用project_startproject_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'.

1 个答案:

答案 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()