2014-03-25 18 views
0

如何比較使用月份,日期和年份的最後字符而不是使用日期時間,示例是Textbox3大於textbox9,因爲Textbox3 day = 26和textbox9天= 25如何比較dateformat而不是使用時間

mycode的:

'in my case I have 2 Textbox. 
'Date format: hh.mm MM/DD/YYYY 

'Textbox3= 02.02 03/26/2014 
'TextBox9= 21.01 03/25/2014 

If Val(Strings.Left(TextBox9.Text.Trim, 5)) < Val(Strings.Left(Textbox3 .Text.Trim, 5))Then 
    TimeError.ShowDialog() 
End If 
+1

爲什麼不使用['DateTime'](http://msdn.microsoft.com/zh-cn/library/system.datetime.aspx)?它有什麼問題? – Styxxy

+1

做你自己的忙,不要使用'Val' – Plutonix

+0

你爲什麼要做這樣的事情?我能想到的唯一原因是這是家庭作業,你必須這樣做,因爲他們試圖教你字符串操作。是這樣嗎? – jmcilhinney

回答

0

在vb.net您可以comopare日期如下:

dim date1 as date = cdate(Textbox1.text) 
dim date2 as date = Date.now() 


    if date1.date=date2.date then .... 

和幾個月這樣

if date1.month=date2.month then ... 
1

真的,最快,最可靠,最有效的方法是將值解析爲DateTime。並從那裏退後一步,從文本框中獲取日期時間的最快,最有效的方法是使用DateTimePicker control

但如果這不是一種選擇,我們可以建立在代碼中,我給你最後一次:

Dim temp1() As String = Textbox3.Text.Trim().Split(" .".ToCharArray()) 
Dim temp2() As String = Textbox9.Text.Trim().Split(" .".ToCharArray()) 

If DateTime.Parse(temp2(2)) < DateTime.Parse(temp1(2)) Then 
    TimeError.ShowDialog() 
End If 

我補充一點,你可能想也有代碼的情況下進行比較的時候值日期部分相等。以此爲出發點,您應該可以自行編寫該代碼。

相關問題