cjinsocal581
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?
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?
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")
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.N ow.ToShort TimeString )
just produced:
11:30 PM
on my system.
The line:
Debug.WriteLine(DateTime.N
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.N ow.ToShort TimeString )
Debug.WriteLine(DateTime.N ow.ToStrin g("t", Nothing))
Debug.WriteLine(DateTime.N
Debug.WriteLine(DateTime.N
both of those (although I used a msgbox instead of debug.writeline) give me "06:35"
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.
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.