Search code examples
vb.netdatevisual-studio-2012

Subtracting days from date


I'm struggling in vein to work out how to remove 5 days from today's date...

I have the following simple code that searches compares the result of a text file array search and then compares them to today's date. If the date within the text file is older than today then it deletes, if not it doesn't.

What i want though is to say if the date in the text file is 5 days or older then delete.

This is being used in the English date format.

    Sub KillSuccess()
    Dim enUK As New CultureInfo("en-GB")

    Dim killdate As String = DateTime.Now.ToString("d", enUK)

    For Me.lo = 0 To UBound(textcis)
        If textcis(lo).oDte < killdate Then
            File.Delete(textcis(lo).oPath & ".txt")
        End If
    Next

End Sub 

Thanks


Solution

  • You can use the AddDays method; in code that would be something like this:

    Dim today = DateTime.Now
    Dim answer = today.AddDays(-5)
    

    msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx

    Which would make your code

    Sub KillSuccess()
    
        Dim killdate = DateTime.Now.AddDays(-5)
    
        For Me.lo = 0 To UBound(textcis)
            If textcis(lo).oDte < killdate Then
                File.Delete(textcis(lo).oPath & ".txt")
            End If
        Next
    End Sub