2012-08-27 245 views
2

可以說,我的日期是:使用Vb.net當前日期減去日期

Dim theDate As DateTime = DateTime.ParseExact("2013-04-10 21:34 PM", "yyyy-MM-dd HH:mm tt", CultureInfo.InvariantCulture) 


IM格式:

"yyyy-MM-dd HH:mm tt" 

我想獲得當前的日期和財產以後做類似:

Dim theDate As DateTime = DateTime.ParseExact("2013-04-10 21:34 PM", "yyyy-MM-dd HH:mm tt", CultureInfo.InvariantCulture) 
Dim currentTime As System.DateTime = System.DateTime.Now 
dim Result as new datetime 
Result = currentTime.Date - theDate 

更新:

我想:

Dim currentTime As System.DateTime = System.DateTime.Now 
Dim date1 As New System.DateTime(2013, 4, 10, 21, 34, 10) 
Dim date2 As New System.DateTime(currentTime.Year, currentTime.Month, currentTime.Day, currentTime.Hour, currentTime.Minute, currentTime.Second) 
Dim diff1 As System.TimeSpan 
diff1 = date2.Subtract(date1) 
MsgBox(diff1.ToString) 
+1

你需要一個'TimeSpan' –

回答

4

一個TimeSpan可以讓你減去日期。

DateTime.Subtract Method (TimeSpan)

Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0) 
Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0) 

Dim diff1 As System.TimeSpan 
' diff1 gets 185 days, 14 hours, and 47 minutes. 
diff1 = date2.Subtract(date1) 

MsgBox(diff1.Days) 
MsgBox(diff1.Hours) 
MsgBox(diff1.Minutes) 

這裏是所有members of the TimeSpan class

+0

謝謝兄弟,diff1.XYZ(年,月,日,等日期範圍) 'XYZ' 是不是'System.TimeSpan'的成員 – ffs

+0

看看我的更新,告訴我什麼問題 – ffs

+0

嗨,請看看我的編輯。 –