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);
AluedkeAsked:
Who is Participating?
 
cmrobertsonConnect With a Mentor Commented:
DateTime TimeVar = Convert.ToDateTime(txtTime.txt);
TimeVar.Time will contain your time info
0
 
cmrobertsonCommented:
a timespan is an interval of time or the time since midnight so 3:00pm doesn't qualify perhaps convert to datetime instead
0
 
elkhawajahCommented:
How about this?
 DateTime.Parse("03:00 PM").TimeOfDay

TimeOfDay is of type TimeSpan
0
 
McGurk1Commented:
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.
0
All Courses

From novice to tech pro — start learning today.