?
Solved

Time manipulation in VB.NET

Posted on 2005-03-07
12
Medium Priority
?
214 Views
Last Modified: 2010-04-23
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?
0
Comment
Question by:cjinsocal581
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483662
Dim shortTime As String = Format(Now, "h:mm tt") ... that's one way of doing it (I think)
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483691
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")
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 13483873
Or how about simply:

    MsgBox(DateTime.Now.ToShortTimeString)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483874
I tried that but it didnt display the AM/PM part of the time
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483878
I assumed that's what was wanted, as opposed to a 24-hour clock (sorry for the quick postings... bad "chatting" habit)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13483879
Interesting...it does on mine.

The line:

    Debug.WriteLine(DateTime.Now.ToShortTimeString)


just produced:

    11:30 PM

on my system.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483886
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)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13483888
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))

0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483896
both of those (although I used a msgbox instead of debug.writeline) give me "06:35"
0
 
LVL 6

Author Comment

by:cjinsocal581
ID: 13483901
You guys are funny.  Thanks for the quick response.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13483903
Just checked my regional settings though...  my time is set to "HH:mm:ss"  .... hence me losing the 'tt' part
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13483907
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question