Consulting

Results 1 to 5 of 5

Thread: Solved: Need help with Date/Time issue.

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location

    Solved: Need help with Date/Time issue.

    Hey gang,
    I need a little help with a Date/Time issue. More specifically a time issue. I know I could do what I want using a bunch of if/then statements, but wanted to see if there was a better way to do it.

    I am using a very basic approach to extracting the current computer time.

    [vba]mytime=formatdatetime(now,4)[/vba]

    This returns the current computer time in the format of HH:MM;
    example
    ================
    02:41
    12:37
    23:59

    I want to generate a random number between 1-11 (which I know how to do already) and add it to the above time to come up with a new time. Can someone provide some guidance as to whether there is a function or statement that accomplishes this task? If there is no function or statement, can you give some advice on how to code for this hopefully in a way that is shorter than my beginner experience level permits me to do currently.

    Thanks in advance,
    Butch

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    You could just do "+(<RandomNumber>/24)" to add hours, or "+(<RandomNumber>/24/60)" to add minutes.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would use

    mytime = FormatDateTime(Now + Int(24 * Rnd + 1) / 24 + Int(60 * Rnd + 1) / 24 / 60, 4)

    but you might want to think about whether the generated time has to be greater than Now or not.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    XLD,
    I will give your code a try when I get back to work tonight. I am pretty sure for the purpose that I need, I am looking for a future time that would be a random number between 1-11. Thanks for the suggestion.

    Butch

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ACtually, more sensible is

    [vba]

    mytime = FormatDateTime(Now + Int(1440 * Rnd + 1) / 1440, 4)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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