Solved

Converting a string to time

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now