Help with Timespan diff in

Posted on 2014-08-22
Last Modified: 2014-08-23
I'm trying to get the difference of 2 dates in min or seconds.  When I check the dates, they are both correct with the correct time.  When I try to get the difference of the 2, it's not correct.  Am I doing this wrong?

Dim strFileInfo As New System.IO.FileInfo("C:\testfile.txt")  'get file info
            Dim dateDiff As TimeSpan = DateTime.Now.Subtract(strFileInfo.LastWriteTime.Date)    
            MsgBox(strFileInfo.LastWriteTime.TimeOfDay.ToString & "    " & Date.Now.TimeOfDay.ToString & "   " & dateDiff.Seconds.ToString)
            If dateDiff.Seconds >= 120 Then 'if older than 2 min, dont use it.  info is not current
Question by:chadmanvb
    LVL 35

    Expert Comment

    by:Miguel Oz
    I think what you need to use is the TotalSeconds property.
    MsgBox(strFileInfo.LastWriteTime.TimeOfDay.ToString & "    " & Date.Now.TimeOfDay.ToString & "   " & dateDiff.TotalSeconds.ToString)

    Open in new window

    Note: Property Seconds returns only the Seconds component of the TimeSpan.

    Author Comment

    I'm still not getting this to work.  Here is what I get when I do TotalSeconds.

    21:04:49.0220256    21:10:13.5014703   76213.5004702

    when I do seconds I get this.  This will always show me the seconds in the
    21:04:49.0220256    21:10:55.2506448   55

    I'm sure I'm missing something simple here.
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    OK, but you're subtracting two DateTimes, and displaying two TimeOfDays. These are different things. "08/23/2014 00:35:00" is a DateTime; "00:35:00" is a TimeOfDay.
    LVL 35

    Accepted Solution

    You are only comparing date component of LastWriteTime:

    Open in new window

    the line above should be:

    Open in new window

    thus your code should look like:
    Dim strFileInfo As New System.IO.FileInfo("C:\testfile.txt")  'get file info
    Dim now As DateTime = DateTime.Now
    Dim dateDiff As TimeSpan = now.Subtract(strFileInfo.LastWriteTime.Date)    
    MsgBox(strFileInfo.LastWriteTime.ToString("f") & "    " & now.ToString("f") & "   " & dateDiff.TotalSeconds.ToString)

    Open in new window

    Note: If more help required please post the contents of message box.

    Author Closing Comment

    Thanks so much!  I knew it was something simple.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    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 …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now