Solved

make time out of string

Posted on 2014-01-09
9
251 Views
Last Modified: 2014-01-13
I have a database filled with 24 hr clock times in string format.
9:00  12:13  17:42  you get the picture.
Is there an easy way to get the time between 9:00 and 17:42
I can think of ways to split the string around the colon and then figure the elapsed time, but is there and easy way of doing it?  Obviously one can't subtract, because of the silly 60 minutes.

suggestions?

gary
0
Comment
Question by:javagair
[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
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39769913
How about using System.DateTime for both times, and then subtract them. The Result will be a System.TimeSpan.

Here is a small example:
String dateStub = "2014-01-01 "
String strtime1 = "9:00"
String strtime2 = "17:00"
DateTime time1 = DateTime.ParseExact(dateStub + strtime1, "yyyy-MM-dd hh:mm", enUS, DateTimeStyles.None)
DateTime time2 = DateTime.ParseExact(dateStub + strtime2, "yyyy-MM-dd hh:mm", enUS, DateTimeStyles.None)
TimeSpan diff = time2 - time1
' then use diff.Hours and diff.Minutes to get the time difference

Open in new window

0
 

Author Comment

by:javagair
ID: 39772090
so you are just adding a random date to the time and then using the timespan to look only at the times?
good thinking I will try it

thanks

gary
0
 

Author Comment

by:javagair
ID: 39772179
the enUs is giving me a problem
imported systems.globalization which gave me the DateTimeStyles but enUS still has an error

gary
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:chaau
ID: 39772544
You can use null in place of enUS, like this:
DateTime time2 = DateTime.ParseExact(dateStub + strtime2, "yyyy-MM-dd hh:mm", null, DateTimeStyles.None)

Open in new window

0
 

Author Comment

by:javagair
ID: 39777594
If you put null in the expression, compiler says null is not defined, use system.dbnull which if you do compiler says type system can not be used in expression.
Null was defined in the old Vb, why not now?

datestub = "#2014-01-01 "    'I added the # seems to want one also tried it without the #
Dim cultures() As CultureInfo = {New CultureInfo("en-US")}
savestart = String.Copy(DataGridView1.Item(1, i).Value())  ' this is 8:00
Dim time1, time2, time3, time4 As DateTime  ' time1 defaults to #12:00 AM#

put in a break  on the next statement and run
 time1 = DateTime.ParseExact(datestub & savestart & " AM#", "yyyy-MM-dd hh:mm", cultures(0), DateTimeStyles.None)

so datestub is #2014-01-01 savestart is 8:00  
we end up with #2104-01-01 8:00 AM# or the same with the # and the AM removed.
I get the same message string is in wrong format.

gary
0
 
LVL 25

Expert Comment

by:chaau
ID: 39777644
Sorry, it is VB. You need to use Nothing instead of Null.

Anyway, try this (do not use hashes):
DateTime time2 = DateTime.ParseExact(dateStub + strtime2, "yyyy-MM-dd h:mm", CultureInfo.InvariantCulture)

Open in new window

The main thing here is a single "h" in the time format and "CultureInfo.InvariantCulture"
0
 

Author Comment

by:javagair
ID: 39777720
okay almost got it to work.
anytime greater than 12:00 give the invalid datetime error again.
if I change all times to less than 12 then everything is okay.
So it still does not understand Military time, 24 hr clock

thanks for everything

gary
0
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 39777787
Sorry, my mistake again. You needed to use Uppercase H. The lowercase h is for 12 hour format.
DateTime time2 = DateTime.ParseExact(dateStub + strtime2, "yyyy-MM-dd H:mm", CultureInfo.InvariantCulture)

Open in new window

                                           
BTW, if your hours have leading zero, like 08:24, then you need to use HH:
DateTime time2 = DateTime.ParseExact(dateStub + strtime2, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture)

Open in new window

0
 

Author Comment

by:javagair
ID: 39778015
thanks
should have noticed that.

works great.

gary
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

635 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