Convert 6 numeric (military time) to standard time?

How can I convert in VB.NET (Newbie question)

Page 1 of 1

1 Replies - 22244 Views - Last Post: 06 October 2007 - 08:49 PM Rate Topic: -----

#1 mlee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-October 07

Convert 6 numeric (military time) to standard time?

Post icon  Posted 04 October 2007 - 10:45 AM

I am reading a database from an IBM I-series (AS/400 and no jokes please). The field in the database is a "time" field and is 6 numeric with 0 decimals. It is in military time (also called 24-hour time) in the form of:


That is military time format for 6:37 PM and 12 seconds (6:37:12 PM)

Is there a way in VB.NET that I can convert this numeric field from military to standard time so that I would end up with:

6:37 PM ???

Thank you and please forgive my newbie-ness. I'm working at it though!

Is This A Good Question/Topic? 0
  • +

Replies To: Convert 6 numeric (military time) to standard time?

#2 orcasquall  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 158
  • Joined: 14-September 07

Re: Convert 6 numeric (military time) to standard time?

Posted 06 October 2007 - 08:49 PM

Try converting the time numeral into a string like
Dim sTime As String = "183712"

I don't know how you retrieve and store the time numeral immediately after you get it from the database. Just convert it to string.

Then do this
Dim dt As DateTime
dt = DateTime.ParseExact(sTime, "HHmmss", Nothing)
Dim sTheTimeFormatYouWant As String = String.Empty
sTheTimeFormatYouWant = dt.ToString("h:mm tt")

The ParseExact() function will then take your time string and change it into a DateTime type. (The day, month and year will be that day when it's run, as in DateTime.Now or DateTime.Today) You might want to enclose the ParseExact() in a Try-Catch clause in case your time numeral is incorrect and the parsing fails.

"HH" represents the zero-padded 24-hour hour
"mm" represents the zero-padded minute
"ss" represents the zero-padded second

"h" represents the 12-hour hour (no zero padding). Double "h" for zero-padding.
"tt" represents the time of day (AM or PM)

You might want to refer to the MSDN documentation for more details. Search for "format strings".

Hope this helps!
Was This Post Helpful? 1

Page 1 of 1