简体   繁体   English

在Vb.net中检查日期

[英]Date Check in Vb.net

I have a problem where in excel one column contains a unique number in the form : mmdd followed by serial number 01,02.. For eg: 101201(10:month, 12:today's date, 01:first entry), 101202. Now, what I need is that my form in vb.net should take the last entered data (for eg: 101202) check if it is today's date. 我遇到一个问题,其中在excel中,一列包含以下格式的唯一编号:mmdd,后跟序列号01,02。例如:101201(10:month,12:today's date,01:first entry),101202。 ,我需要的是vb.net中的表单应采用最近输入的数据(例如:101202),检查是否为今天的日期。 If yes, then it should add 1 and display it in a message box.(yes, then 101203 should be displayed). 如果是,则应加1并显示在消息框中(是,然后应显示101203)。 If not, then it should take current date and start with 01 (101301 in case, the date is 13/10/2016). 如果不是,则应采用当前日期,并以01开头(如果情况为13/10/2016,则为101301)。 I managed to get the data from previous row. 我设法从上一行获取数据。 But how should I check it with date? 但是我应该如何检查日期呢? Please help! 请帮忙!

   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

Put this where you have written 'this is wrong and I know it's wrong 将此放在您写的地方'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 basically grabs the first 4 digits from r so now you can compare this to todays date. rDate基本上从r抓取前4位数字,因此现在您可以将其与今天的日期进行比较。 I have also replaced todays date with t because you have t already using today's date in the required format. 我也换成今天的日期与t ,因为你有t已经在使用今天的日期以所需的格式。

Also naming variables as t and r makes it hard to understand what they are used for. 另外,将变量命名为tr使得很难理解它们的用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM