Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3004
  • Last Modified:

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);
0
Aluedke
Asked:
Aluedke
  • 2
1 Solution
 
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
 
cmrobertsonCommented:
DateTime TimeVar = Convert.ToDateTime(txtTime.txt);
TimeVar.Time will contain your time info
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now