2013-04-16 387 views
0

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

數據採集器需要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'. 
+0

你是什麼意思的「完成天數」? –

+4

想一想你正在做什麼 - 試圖劃分_dates_。那有什麼意思? – Oded

+0

查看[TimeSpan](http://msdn.microsoft.com/zh-cn/library/system.timespan.aspx) - 這應該可以幫助您獲得所需的結果。從EndDate減去Startdate會給你一個代表100%的TimeSpan。 – Filburt

回答

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() 
+0

這不會執行整數除法嗎? (這將在C#中)我會寫'Dim percentage = CDbl(daysToNow)/ sumDays * 100' –

+0

TotalDays是雙倍,我在C#中測試了這個代碼,然後將其轉換爲ti vb.net :-) –