Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Treying to convert string to TimeSpan in C#

Posted on 2012-03-30
3
Medium Priority
?
1,608 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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