Forums » .NET » .NET »

How to convert a string to a timespan in C#.net?


Posted Date: 02 Feb 2008      Posted By:: Harsh     Member Level: Gold  Member Rank: 0     Points: 2   Responses: 1

I have a string which I want to convert toa time span object
eg.
string sDuration = "00:18:09";

Now I want to assign this value to a time span object.
eg.
TImeSpan ts = new TimeSpan();
ts = (TimeSpan)sDuration;

but this type cast is not working.


Responses

#259714    Author: satyaprakash      Member Level: Silver      Member Rank: 0     Date: 04/Jul/2008   Rating: 2 out of 52 out of 5     Points: 4

TimeSpan timeSpan = new System.TimeSpan();
bool isTimeSpan = System.TimeSpan.TryParse(sDuration , out timeSpan); (if u r working in 2003 u wont have TryParse..u can use simply Parse method.TryParse returns false if the input string is not in correct format .)..

Try it.It works

Rohan


 
Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.





  • Return to Discussion Forum
  • Start new thread


  • Subscribe to Subscribers
    Active Members
    Today
      Last 7 Daysmore...

      Awards & Gifts
      Talk to Webmaster Tony John

      Online Members

      Pramod
      More...
       
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India

      
      
      
      

      Click the "Follow" button above to follow Tony John