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
Solved

Converting a string to time

Posted on 2009-07-11
4
229 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
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 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 85

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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