Link to home
Start Free TrialLog in
Avatar of cjinsocal581
cjinsocal581Flag for United States of America

asked on

Time manipulation in VB.NET

Don't know if this is simple but I need it pretty quick. so 500 to the first working solution.

I need to trim the Now() to short time.

In other words, if I was to call Now(), it would bring back "3/7/2005 9:01:36 PM"

I need to return the following: "9:01 PM"

any ideas?
Avatar of S-Twilley
S-Twilley

Dim shortTime As String = Format(Now, "h:mm tt") ... that's one way of doing it (I think)
If you want a leading zero on the hour (although you didn't give it in your example)

Dim shortTime As String = Format(Now, "hh:mm tt")

==========================
..... you can also use the following

Dim shortTime as String = Now.ToString("h:mm tt")

or for the leading zero version

Dim shortTime as String = Now.ToString("hh:mm tt")
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried that but it didnt display the AM/PM part of the time
I assumed that's what was wanted, as opposed to a 24-hour clock (sorry for the quick postings... bad "chatting" habit)
Interesting...it does on mine.

The line:

    Debug.WriteLine(DateTime.Now.ToShortTimeString)


just produced:

    11:30 PM

on my system.
Well im in england... so it's AM over here but it left that part out... "06:33".    Not sure if it's cos my settings are defaulted to a 24 hour clock or not for short time...   or it just ignores AM (Im guessing the first one)
According to the help file, these two should give the same result (yet another way to get the short time):

    Debug.WriteLine(DateTime.Now.ToShortTimeString)
    Debug.WriteLine(DateTime.Now.ToString("t", Nothing))

both of those (although I used a msgbox instead of debug.writeline) give me "06:35"
Avatar of cjinsocal581

ASKER

You guys are funny.  Thanks for the quick response.
Just checked my regional settings though...  my time is set to "HH:mm:ss"  .... hence me losing the 'tt' part
Yeah...was just looking into the ToShortTimeString() function and it uses the regional settings so I guess your first proposed method of

    Dim shortTime As String = Format(Now, "hh:mm tt")

is the best solution if you want it to work on any system regardless of the regional settings.