Link to home
Start Free TrialLog in
Avatar of Aluedke
Aluedke

asked on

Convert/Parse string to Timespan

I have a text box (txtTime) that retrieves a user inputted value as time (ex. 3:00 PM).  I have a field (StartTime) in my database of type time(7) to store this value in.  

Unfortunately, I'm unable to convert the string from the textbox to this timespan value.  Any help would be appreciated.  

txtTime.Text  is equal to 3:00 PM (the time can change), but the following doesn't work.  

 Timespan Time = Timespan.Parse(txtTime.Text);
Avatar of cmrobertson
cmrobertson
Flag of United States of America image

a timespan is an interval of time or the time since midnight so 3:00pm doesn't qualify perhaps convert to datetime instead
How about this?
 DateTime.Parse("03:00 PM").TimeOfDay

TimeOfDay is of type TimeSpan
ASKER CERTIFIED SOLUTION
Avatar of cmrobertson
cmrobertson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had the same issue.  However, elkhawajah's answer is the one that worked for me.  I use VS 2008 and SS 2008 which allows storing as a Time type.  When I tried cmrobertson's solution, it would not convert a string to a Timespan.  However, when I used the following, it worked fine.

Dim tsTime as Timespan = DateTime.Parse(txtTime.Text).TimeOfDay

Just wanted to add the comment for anyone else seeking an answer to storing to the Time type in SS 08.