DateDiff Does Not Return True

  • 13 years ago
    dim dtefiledate as datetime
    dim stroutofdate as string
    
    try
    fileopen(1, application.startuppath & "\Dates\6Months\Dates.iss", OpenMode.Input)
    
    Do While Not EOF(1)
    Input(1, dtefiledate)
    FileClose(1)
    
    if DateDiff (DateInterval.Month,Now,dtefiledate) >= 6 then
           stroutofdate = stroutofdate & "," & dtefiledate
           MessageBox.Show(stroutofdate)
    End If
    Loop
    
    The first date in my file is #1/1/2007# .... this is august meaning the current
    month is 8...... 8-1 = 7
    this should return true....why not?
  • 13 years ago

    Actually it should return false :)  A quick test would show the value being returned is -7 not 7.  The conclusion would be the function is returning Date2 - Date1.  The documentation found on the MSDN library would also confirm this. 

    "Required. Date. The two date/time values you want to use in the calculation. The value of Date1 is subtracted from the value of Date2 to produce the difference. Neither value is changed in the calling program. "

    When in doubt always check what value IS being returned, don't just assume.

    **EDIT**
    Calling FileClose(1) inside your loop will close the file.  When you call EOF(1) the next time it will throw an error.  You shouldn't close the file until your done with it.  Also, if you haven't explored the StreamReader and StreamWriter classes I would recommend looking into them.  I just find the stream classes a better way to read/write to files.  Plus in my experience they are more efficient.

  • 13 years ago

    haha .... I knew you were gonna say something about StreamReader .... you told me the same thing about StreamWriter when I was working on a previous post lol

    so I if I flop Now and dtefiledate .... this should fix the problem?

  • 13 years ago

    Yup if you flip the dates you should be all set.

    I guess if I mentioned it before you must not have taken my advice :) Whenever I see FileOpen, FileClose I just shiver...Anyway that's up to you.  I'll try to remember I've already mentioned the stream classes to you so I don't tell you again in some later post. 

  • 13 years ago

    I will be a good student from now on :)

    I actually did change all my code to StreamWriter in the last project I did when you first told me about it ... I guess I sort of forgot lol

    When I attended college a couple years back they were teaching VS2003, and the teacher at the time had us using FileOpen, FileClose .... so having done it for 3.5 months I gotta learn to break the habit of doing it in my own personal projects.... if only you could have been there to say "No! No! No! That is all wrong!" maybe drop a pc on her head for emphasis lol

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”