Solved

Treying to convert string to TimeSpan in C#

Posted on 2012-03-30
3
1,306 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
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 36
RLDC Reporting in Visual studio 11 16
No Data for DropDown List 2 25
sql 2008 how to table join 2 16
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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