Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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?
0
cjinsocal581
Asked:
cjinsocal581
  • 7
  • 4
1 Solution
 
S-TwilleyCommented:
Dim shortTime As String = Format(Now, "h:mm tt") ... that's one way of doing it (I think)
0
 
S-TwilleyCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Or how about simply:

    MsgBox(DateTime.Now.ToShortTimeString)
0
Industry Leaders: 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!

 
S-TwilleyCommented:
I tried that but it didnt display the AM/PM part of the time
0
 
S-TwilleyCommented:
I assumed that's what was wanted, as opposed to a 24-hour clock (sorry for the quick postings... bad "chatting" habit)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Interesting...it does on mine.

The line:

    Debug.WriteLine(DateTime.Now.ToShortTimeString)


just produced:

    11:30 PM

on my system.
0
 
S-TwilleyCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
S-TwilleyCommented:
both of those (although I used a msgbox instead of debug.writeline) give me "06:35"
0
 
cjinsocal581Author Commented:
You guys are funny.  Thanks for the quick response.
0
 
S-TwilleyCommented:
Just checked my regional settings though...  my time is set to "HH:mm:ss"  .... hence me losing the 'tt' part
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now