[英]Date Check in Vb.net
我遇到一個問題,其中在excel中,一列包含以下格式的唯一編號:mmdd,后跟序列號01,02。例如:101201(10:month,12:today's date,01:first entry),101202。 ,我需要的是vb.net中的表單應采用最近輸入的數據(例如:101202),檢查是否為今天的日期。 如果是,則應加1並顯示在消息框中(是,然后應顯示101203)。 如果不是,則應采用當前日期,並以01開頭(如果情況為13/10/2016,則為101301)。 我設法從上一行獲取數據。 但是我應該如何檢查日期呢? 請幫忙!
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
xlWorkBook = xlApp.Workbooks.Open("C:\Users\Desktop\testing.xlsx")
'xlApp.Visible = True
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
Dim row As Long
row = 1
With xlWorkSheet
While .Cells(row, 2).value IsNot Nothing
row = row + 1
End While
'MsgBox(row - 1)
End With
Dim r As Excel.Range
Dim t As String = Format(Today, "MMdd")
If xlWorkSheet.Cells(row - 1, 4).value Is Nothing Then
Me.txtQuote.Text = t & "01"
Else
r = xlWorkSheet.Cells(row - 1, 4)
Me.txtQuote.Text = (r.Value + 1)
End If
'this is wrong and I know it's wrong
If r.Value = Date.Today Then
MsgBox("true")
Else
MsgBox("false")
End If
End Sub
將此放在您寫的地方'this is wrong and I know it's wrong
Dim rDate As String = CStr(r.Value).Substring(0, 4)
If rDate = t Then
MsgBox("true")
Else
MsgBox("false")
End If
rDate
基本上從r抓取前4位數字,因此現在您可以將其與今天的日期進行比較。 我也換成今天的日期與t
,因為你有t
已經在使用今天的日期以所需的格式。
另外,將變量命名為t
和r
使得很難理解它們的用途。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.