Solved

Converting a string to time

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now