Results 1 to 3 of 3

Thread: [RESOLVED] dateTime arithmetic

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Resolved [RESOLVED] dateTime arithmetic

    Hi,

    I'm trying to subtract 2 dates.: currentdate and a maxDate value that I have. If the difference between the 2 dates is 180 days then I want a messagebox to show up.

    Here's the code I have so far. Everything works except for the "currentDate - maxDate > 180" part. I get an error on that part.

    Code:
            Dim maxDate As DateTime = "1/1/1900"
            Dim x As Integer = 0
            Dim currentDate As DateTime = DateTime.Now
    
            For x = 0 To Me.DataGrid1.VisibleRowCount - 1
                Try
                    If Not IsDBNull(DataGrid1.Item(x, 2).ToString) Then
                        If CDate(DataGrid1.Item(x, 2)) > maxDate Then
                            maxDate = (DataGrid1.Item(x, 2))
                        End If
                    End If
                Catch ex As Exception
    
                End Try
            Next
    
    
            If currentDate - maxDate > 180 Then
                Msgbox("error")
            End If

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    72

    Re: dateTime arithmetic

    Solved:

    Code:
    Dim a As System.TimeSpan
            a = currentDate.Subtract(maxDate)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] dateTime arithmetic

    You don't actually have to use the Subtract method in more recent version because DateTime and TimeSpan support straight arithmetic, e.g.
    vb.net Code:
    1. If (currentDate - maxDate).Days > 180 Then

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width