Results 1 to 11 of 11

Thread: [RESOLVED] How can I get the local time on the computer?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] How can I get the local time on the computer?

    Hi there, I am finishing up a math prog and I am trying to get it to save the students marks to a textfile, and I include a bit of information about the test in the textfile file name. I got the calendar date, but would like to include the hour and min, and possibly the AM Or PM.

    Would anyone know of a way to grab the time and put it into a label caption? Thank you!!

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

    Re: How can I get the local time on the computer?

    You can get the date using the Date function, and the time using the Time function, or both together using Now.

    To store any of those into a String in a particular format, use the Format function.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How can I get the local time on the computer?

    Ok LOl I didn't know it was that simple.

    So my code looks like this

    TimeLBL.caption = Format(Time, (Now))

    So I get the date, 06/04/2012, and the time 10:00:02AM.

    Now I like that, I like that a lot, but what would I have to do to just get the time?

    Also, are you allowed colons in filenames? Thanks!

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How can I get the local time on the computer?

    I just checked and it won't let me use colons in a filename. Would there be a way to get the time, but have it formatted to give me time like this..

    10h 37minAM?

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

    Re: How can I get the local time on the computer?

    Something like this: hour(time) & "h " & minute(time) & "min" & format(time,"AM/PM")

    But a more reasonable solution for filenames may be to use hour/time with leading zeroes:
    format(time,"hhnn")
    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}

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    16

    Re: How can I get the local time on the computer?

    Easiest way, no APIs or anything.

    Code:
    Debug.Print TimeValue(Now) 'Fectches the Current Computer Time
    Debug.Print DataValue(Now) 'Fetches the current computer date
    Theres many ways to do this, such as the format function, but i find this the easiest and as you can see takes up no room.

    Hope this was helpful,
    Cypher

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How can I get the local time on the computer?

    Wonderful, thank you!

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    16

    Re: [RESOLVED] How can I get the local time on the computer?

    No problem, hope you can incorperate this into your project.

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

    Re: [RESOLVED] How can I get the local time on the computer?

    I don't think that's particularly clever. For example:
    Code:
    Debug.Print DateValue(Now)
    compiles as
    Code:
    Debug.Print CStr(CDate(Fix(CDate(CStr(Now)))))
    Hardly "takes no room."

  10. #10
    Junior Member
    Join Date
    Mar 2010
    Posts
    16

    Re: [RESOLVED] How can I get the local time on the computer?

    not clever? it doesn't take no room at all, and get the current time or date of the computer.

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

    Re: [RESOLVED] How can I get the local time on the computer?

    Just use the Time() function then as suggested in post #2. It is even more brief and it doesn't go through as many gyrations.

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