• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1695
  • Last Modified:

Treying to convert string to TimeSpan in C#

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
jazzcatone
Asked:
jazzcatone
1 Solution
 
Chinmay PatelEnterprise ArchitectCommented:
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
 
käµfm³d 👽Commented:
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
 
jazzcatoneAuthor Commented:
Perfect. Thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now