?
Solved

Treying to convert string to TimeSpan in C#

Posted on 2012-03-30
3
Medium Priority
?
1,509 Views
Last Modified: 2012-04-04
Is there a reason why this doesn't work ? It says its not recognize as a valid TimeSpan. In the end I need to insert into a SQL Server time(7) field. How to do ? Any advice would be most appreciated.

TimeSpan timeSpanCompTime = new System.TimeSpan();
timeSpanCompTime=TimeSpan.Parse("08:00 AM");



Jason
0
Comment
Question by:jazzcatone
[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
3 Comments
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37788134
Hi Jason,

I don't have access to an IDE right now but a TimeSpan will not take 8.00 AM as a valid parameter. It has to be a timespan, and you are specifyting time itself.

It can be 8 hours it can not be 8.00 AM.

Check : http://msdn.microsoft.com/en-us/library/se73z7b9(v=vs.71).aspx

Regards,
Chinmay.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37788313
Use DateTime.Parse and grab the value of TimeOfDay from the resulting DateTime object.

e.g.

TimeSpan timeSpanCompTime = new System.TimeSpan();
timeSpanCompTime=DateTime.Parse("08:00 AM").TimeOfDay;

Open in new window

0
 

Author Closing Comment

by:jazzcatone
ID: 37806675
Perfect. Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 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