Solved

Convert/Parse string to Timespan

Posted on 2009-03-29
4
2,969 Views
Last Modified: 2012-05-06
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
Comment
Question by:Aluedke
  • 2
4 Comments
 
LVL 7

Expert Comment

by:cmrobertson
ID: 24014341
a timespan is an interval of time or the time since midnight so 3:00pm doesn't qualify perhaps convert to datetime instead
0
 
LVL 1

Expert Comment

by:elkhawajah
ID: 24014348
How about this?
 DateTime.Parse("03:00 PM").TimeOfDay

TimeOfDay is of type TimeSpan
0
 
LVL 7

Accepted Solution

by:
cmrobertson earned 500 total points
ID: 24014364
DateTime TimeVar = Convert.ToDateTime(txtTime.txt);
TimeVar.Time will contain your time info
0
 

Expert Comment

by:McGurk1
ID: 35190580
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

831 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