Solved

Converting a string to time

Posted on 2009-07-11
4
231 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 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 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
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…

687 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