Help with Timespan diff in vb.net

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
chadmanvbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
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.
0
chadmanvbAuthor Commented:
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 date.now.timeofday
21:04:49.0220256    21:10:55.2506448   55

I'm sure I'm missing something simple here.
0
käµfm³d 👽Commented:
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.
0
Miguel OzSoftware EngineerCommented:
You are only comparing date component of LastWriteTime:
DateTime.Now.Subtract(strFileInfo.LastWriteTime.Date)  

Open in new window

 
the line above should be:
DateTime.Now.Subtract(strFileInfo.LastWriteTime) 

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chadmanvbAuthor Commented:
Thanks so much!  I knew it was something simple.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.