Results 1 to 16 of 16

Thread: [RESOLVED] Time difference giving incorrect results vb 6

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Resolved [RESOLVED] Time difference giving incorrect results vb 6

    Dim Time As String
    Time = "09:58 AM"

    Minutes = DateDiff("n", Time, Format(Now, "short time"))
    when ran at 8:57PM
    the minutes say 661, but should be 780
    what is wrong ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Time difference giving incorrect results vb 6

    Syntax

    DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
    Why would you turn a Date value (the Now() function) into a String and then pass it as a Date parameter causing VB6 to laugh at you, say "ok guy," and just try to convert your String back into a Date in order to call DateDiff()?

    Why haven't you properly parsed your Time String value into a Date value instead of giving VB another chuckle as you rely on coercion to succeed?

    Stop trying to use Strings.


    BTW:

    Code:
    Debug.Print DateDiff("n", #9:58:00 AM#, #8:57:00 PM#)
    Gives 659 minutes, just as one would expect.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Time difference giving incorrect results vb 6

    There is no telling what a user (even yourself) might have the Short Time format defined to be. This is for display only.

  4. #4

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    Actually I am trying to modify code written by a coder that is more advanced that my capabilities.
    this is a reminder program that uses a mshflegrid to hold values. the coder scans the date/time column to get results(Shown in a Next Reminder Column)
    I am trying to determine when a reminder is due to pop up a message box

    i posted Dim Time As String
    Time = "09:58 AM" just to try get an answer. the dates and times are in a mshflexgrid that has hidden columns. this seems to be accurate
    this code seems to fix the issue
    iMinutes = DateDiff("n", Format(Now, "short time"), .TextMatrix(2, GRD_TIME_DATE))
    iHours = iMinutes / 60
    displayMinutes = iMinutes Mod 60
    i
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  5. #5

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    The grid looks like this:
    Name:  grid.jpg
Views: 156
Size:  21.9 KB
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Time difference giving incorrect results vb 6

    Well, whoever create that grid also had little clue on how to use dates. Is this data fed from some database table or is it 'static', meaning a user would have to populate a row using VB controls?

    But, let's say it is all fine and dandy. Notice how the dates and times are displayed. (As dile states, these are STRINGS being displayed because that is how mshFlexGrids (and other controls) DISPLAY dates. So, first, in your code (which I didn't even look at), convert those strings back to dates and times as necessary. THEN do your dateDiff() function.

    Just curious though....do you think someone is going to be watching your computer screen 24X7? I mean, that message box will (supposedly) be displayed when it is time for mom to take her medicine (in your example, each day at 9:58 (presumably an NFL phrase, "2-Minute Warning" for her 10 O'clock pop-a-pill requirement). Unless someone is sitting there at 9:58, she may miss her dosage and that would be on YOU...if this is just a homework assignment (Which I doubt because you said someone else created this), then you need to take the advice of dile and use DateDiff() as intended....using 'dates', not strings.

  7. #7

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    Thanks for your feedback. i plan on adding a sound with the messagebox the code works fine and is hooked to a timer.and is updated every ten seconds.
    the coder wrote this using strings and it works. to enter data it uses date,time picker. and saves to a file and also uses rtf files with a name like this
    gstrTaskFileName 'Example Left( gTaskNamePrefix , 2) & Format(Now, "YYYYMMDDHHMMSS") & ".rtf" the name is in a hidden column. and has details
    of the event eg;doctors address, phone etc
    i am just trying to modify it
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Time difference giving incorrect results vb 6

    You know when that (first) message box is displayed, all other processes in your program stop until someone presses that OK button (unless you have the box automatically disappear after a certain period of time). So, "every ten seconds" will pause after the display of that box.

    If you say it 'works', why are you trying to modify it? I thought you wrote that the calculations were incorrect. Does it work, or not? But still, I see issues with you using strings as dates (as dile pointed out) and the issue of the messagebox. It just doesn't seem to be a 'practical' application.

  9. #9

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    The programmer wrote it where if a alert is due it opens another flexgrid with no way to mark it as done, instead of message boxes
    when i say it works i mean it properly displays times and dates in the Next Reminder column
    example:
    The 5 hours 31 minutes is correct
    in a minute it correctly changes to 5 hours and 30 minutes etc
    I Am trying to determine when a reminder is due to display a custom msgbox
    this does not work
    iMinutes = DateDiff("n", Format(Now, "short time"), CDate(.TextMatrix(lngReminder, GRD_TIME_DATE) + 1))
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Time difference giving incorrect results vb 6

    Quote Originally Posted by isnoend07 View Post
    iMinutes = DateDiff("n", Format(Now, "short time"), CDate(.TextMatrix(lngReminder, GRD_TIME_DATE) + 1))
    There are two problems with that line, even if it did give the correct result.

    The first is that the Format(Now, "short time") does a lot of unnecessary work (it converts a Date value to a String in a particular format, which then needs to be converted back to a Date value by DateDiff), part of which is unsafe (converting a String value to a Date).

    Converting a String value to a Date is unsafe unless the format of the String is totally unambiguous ("short time" is not). As it is unsafe, even if it gives you the right answer, it does not mean it will if circumstances change at all (eg: the day changes).

    In this case you can easily avoid it by simply using the Time function instead (eg: DateDiff("n", Time, ... ), and removing/renaming your Time variable if you still have it.


    The value you get from .TextMatrix is also unsafe to convert to a Date, so if the data isn't stored as a Date elsewhere, you should write code to safely convert it from your String format to a Date.

    For an example and explanation of how to do that, see the "How to safely convert a String to a Date" section of this FAQ article: Classic VB - Why are my dates not working properly?

  11. #11

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    Never realized dates-times were so sensitive-involved. good thing this is just for me and my kids. looks like i am stuck with strings as all the calculations are done on data in a mshflexgrid. Originally started using this when my sister got cancer. To remind her of doc Appointments, take medicines, put her garbage to curb etc She was on oxygen and at her computer all day every day. she died Jan 7th and now i am trying to modify it to use myself Seems i can not overcome some issues, without help
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Time difference giving incorrect results vb 6

    Well, if I were you, I would do ALL my calculations/warnings based upon the fields in my database, where the 'appointment/birthdate' information should be stored as date/time. Forget using the grid to prompt, use the DB. Use the grid ONLY for display of the information. That way, you won't need to convert strings to dates and vice versa (except maybe to display the dates in the format you desire to see them in the grid).

    Try that, and if you need help, come back with issues you have....

  13. #13

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    Thanks the data is stored in a textfile no database
    i am trying to parse the data in the Next Reminder column which seems to be accurate to get the info i need
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Time difference giving incorrect results vb 6

    Date/Time values are a bit awkward, but based on my own experience (and more so by questions on the forums) they are easier and less problematic than the alternatives.

    As your date-text seems to be in the format "mm-dd-yyyy", the following modification of the FAQ code for "How to safely convert a String to a Date" should work for you:
    Code:
    Dim MyDate as Date
    Dim TempArray() as String
    
        'Separate the items by the delimiter (in this case "-") 
      TempArray() = Split(.TextMatrix(lngReminder, GRD_TIME_DATE) , "-")
    
      Select Case UBound(TempArray)
      Case 2  'As there are 2 "-" characters, it is probably a date
           'Place each item in the relevant part of DateSerial to build the date
         MyDate = DateSerial(TempArray(2), TempArray(0), TempArray(1))
    
         If Format (MyDate,"MM-DD-YYYY") <> .TextMatrix(lngReminder, GRD_TIME_DATE) Then
            'something went wrong, and we didn't get the right value - you may want to show a message
         End If
      Case 0  'As there are no "-" characters, it is probably a time
           '(code here to convert the time values)
      Case Else
          'unexpected amount of "-" characters, this value cannot be safely converted... you may want to show a message of some sort
      End Select
    I've left a comment there for needing to convert the time based values ("9:58 AM" in post #5), but haven't written the code as I'm not sure how you are formatting it in the first place.

  15. #15

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    Thanks I will use that if my parsing doesnt work
    i am using the code the coder wrote to determine between dates and times
    If InStr(1, .TextMatrix(lngReminder, GRD_TIME_DATE), "M") > 0 'its a time
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  16. #16

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Time difference giving incorrect results vb 6

    this seems to work
    With gridMaster
    If InStr(1, .TextMatrix(lngReminder, GRD_NEXT_REMINDER), "Hours") > 0 Then'it has hours
    MyHours = Left(.TextMatrix(lngReminder, GRD_NEXT_REMINDER), 2)
    ArrayString = Split(.TextMatrix(lngReminder, GRD_NEXT_REMINDER), " ")
    MyMinutes = ArrayString(2)
    Else ' no hours just minutes
    MyMinutes = Left(.TextMatrix(lngReminder, GRD_NEXT_REMINDER), 2)


    End If
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width