?
Solved

Converting a string to time

Posted on 2009-07-11
4
Medium Priority
?
232 Views
Last Modified: 2012-05-07
I'm using the code below but the result is always 00:00:00.

I've also tried
dim newtime as string = DateTime.ParseExact(0920, "h:mm:ss tt", CultureInfo.InvariantCulture) but get the message: String was not recognized as a valid DateTime.
dim dt as date
dim s as string
s="920"
dt = cdate(format(s,"00:00"))

Open in new window

0
Comment
Question by:kirkheaton25
[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
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 24830619
If you try to parse the string as hours and minutes, this will work:

Dim t As DateTime = DateTime.ParseExact("0920", "HHmm", CultureInfo.InvariantCulture)

However, if you have a string like "920", that can not be parsed in a single operation. Although you can format a DateTime value into that format using "Hmm", the same format can't be used to parse a string like that.

In that case you will either have to split the string in hours and minutes and parse as numbers:

Dim s As String = "920"
Dim h As Integer = Integer.Parse(s.Substring(0, s.Length - 2))
Dim m As Integer = Integer.Parse(s.Substring(s.Length - 2))
Dim t As DateTime = new DateTime(h, m 0)

Or, you could insert a separator in the string so that you can parse it:

Dim s As String = "920"
s = s.Substring(0, s.Length - 2) + ":" + s.Substring(s.Length - 2)
Dim t As DateTime = DateTime.ParseExact(s, "H:mm", CultureInfo.InvariantCulture)

Note:
When you parse a string that only contains a time, it will by default get todays date, as a DateTime value alwas contains both a date and time component.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 24830632
Correction:
The constructor for DateTime with three parameters use year, month and day. You have to use the constructor with six parameters to supply hours, minutes and seconds:

Dim t As DateTime = new DateTime(0, 0, 0, h, m, 0)
0
 
LVL 1

Author Closing Comment

by:kirkheaton25
ID: 31602423
Many thanks for your help.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24831028
You could also use TimeSpan.Parse() and add that to a DateTime.  I like the adding a colon approach:

        Dim s As String = "920"
        Dim dt As DateTime = DateTime.Today.Add(TimeSpan.Parse(IIf(s.IndexOf(":") = -1, s.Insert(s.Length - 2, ":"), s)))
        Dim time As String = dt.ToString("HH:mm")
        MessageBox.Show(time)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

771 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