Comparing string to datetime.

Posted on 2009-02-11
Last Modified: 2012-05-06
I have the following:

Dim value As String = String.Empty
 value = filename.Substring(4, 11)
value = 20090210
I need to compare this to datetime and return true if older than 7 days.
Question by:mathieu_cupryk
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi mathieu_cupryk;

    This should do what you need.

    value = "20090210"

    Dim dt As DateTime = New DateTime(Int32.Parse(value.Substring(0, 4)), _
                                      Int32.Parse(value.Substring(4, 2)), _
                                      Int32.Parse(value.Substring(6, 2)))

    Dim ts As TimeSpan = dt - Today

    If ts.Days > 7 Then
        ' Delete your files in here
    End If

    LVL 69

    Expert Comment

    by:Éric Moreau
    dim dt as new date(value.substring(0,4), value.substring(4,2),value.substring(6,2))
    if dt < then
        'older then 7 days
    LVL 62

    Accepted Solution

    Sorry; this line of code

    Dim ts As TimeSpan = dt - Today

    Should be this:

    Dim ts As TimeSpan = Today - dt


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now