Results 1 to 11 of 11

Thread: [RESOLVED] working with 24 hour times

  1. #1

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

    Resolved [RESOLVED] working with 24 hour times

    I have some 24 hour times that i am trying to compare to the users pc time
    task time = 0500 thats 5:AM
    say the users pc time is 3:AM
    The Difference is 2 hours

    task time = 1700 thats 5:PM
    say the users pc time is 4:PM
    The Difference is 1 hour

    how can this be calculated with vb6 ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: working with 24 hour times

    DateDiff can accept 24 or 12 hour times, i.e.,

    MsgBox DateDiff("n","17:00:00", "6:00:00 PM")
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

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

    Re: working with 24 hour times

    Quote Originally Posted by LaVolpe View Post
    DateDiff can accept 24 or 12 hour times, i.e.,

    MsgBox DateDiff("n","17:00:00", "6:00:00 PM")
    thanks
    my problem is i do have a date the 1700 is part of a file name
    Dy1700.rtf with the Dy and .rtf removed i am trying compare the current time to the 1700

    this outputs 2490120

    Dim mytime
    mytime = 1730
    MsgBox DateDiff("n", mytime, "6:00:00 PM")
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: working with 24 hour times

    DateDiff requires the final two parameters to be a date and/or time format. This means you will have to parse the 1700 and determine where to place the colon, i.e., 17:00
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: working with 24 hour times

    Quote Originally Posted by LaVolpe View Post
    DateDiff requires the final two parameters to be a date and/or time format. This means you will have to parse the 1700 and determine where to place the colon, i.e., 17:00
    Actually it requires two Date type values. You can ram Strings in there and rely on coercion to muddle through but that is always a crapshoot due to localization and punctuation issues.

    It is probably better to just write your own functions to parse String data into Date typed data for programs that need to do this. Otherwise you are just Trusting The Force and letting the underlying OLE Automation functions roll the dice, which means you have to accept that it is going to make wrong (sometimes very wrong) guesses. This leads directly to data corruption that can have cumulative destructive effects over time.

  6. #6

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

    Re: working with 24 hour times

    Thanks for your help
    just trying to make a little app to remind my sister when to take her medicine with a messagebox
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: working with 24 hour times

    Quote Originally Posted by isnoend07 View Post
    thanks
    my problem is i do have a date the 1700 is part of a file name
    Dy1700.rtf with the Dy and .rtf removed i am trying compare the current time to the 1700

    this outputs 2490120

    Dim mytime
    mytime = 1730
    MsgBox DateDiff("n", mytime, "6:00:00 PM")
    In this case your myTime variable is a variant that holds a number and that number is then trying to be interpreted as a date. Of course that resulting date is not even close to what you were trying to feed it.

    You could use a string "17:00" or better use a date var with the proper value stuffed in it.

    You should always use the As portion for every variable you use and you should always try and make sure that the variable type you decide on is the correct one for what you are doing.

    Try this
    Code:
    Dim mytime As Date
    mytime = CDate("17:30")
    MsgBox DateDiff("n", mytime, "6:00:00 PM")
    Or better this
    Code:
    Dim mytime As Date
    mytime = CDate("17:30")
    MsgBox DateDiff("n", mytime, CDate("6:00:00 PM"))
    Last edited by DataMiser; Oct 13th, 2015 at 11:27 AM.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: [RESOLVED] working with 24 hour times

    To give you a better idea of why your first attempt did not work try this and see what it gives you
    Code:
    Dim mytime
    mytime = 1730
    MsgBox CDate(mytime)
    You'll noticed that it is a far cry from what you thought you were giving it.

  9. #9

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

    Re: working with 24 hour times

    Quote Originally Posted by DataMiser View Post
    In this case your myTime variable is a variant that holds a number and that number is then trying to be interpreted as a date. Of course that resulting date is not even close to what you were trying to feed it.

    You could use a string "17:00" or better use a date var with the proper value stuffed in it.

    You should always use the As portion for every variable you use and you should always try and make sure that the variable type you decide on is the correct one for what you are doing.

    Try this
    Code:
    Dim mytime As Date
    mytime = CDate("17:30")
    MsgBox DateDiff("n", mytime, "6:00:00 PM")
    Or better this
    Code:
    Dim mytime As Date
    mytime = CDate("17:30")
    MsgBox DateDiff("n", mytime, CDate("6:00:00 PM"))
    I tested the code and it produces 30
    what does 30 mean ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  10. #10

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

    Re: [RESOLVED] working with 24 hour times

    Quote Originally Posted by DataMiser View Post
    To give you a better idea of why your first attempt did not work try this and see what it gives you
    Code:
    Dim mytime
    mytime = 1730
    MsgBox CDate(mytime)
    You'll noticed that it is a far cry from what you thought you were giving it.
    that produces 9/25/1904
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: working with 24 hour times

    Quote Originally Posted by isnoend07 View Post
    I tested the code and it produces 30
    what does 30 mean ?
    30 minutes of course. the "n" is telling it to give your the difference in minutes

    and the other one shows you why you got such a large number because rather than comparing two times that were 30 minutes apart you were using times that were over 100 years apart

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