Solved

Convert/Parse string to Timespan

Posted on 2009-03-29
4
2,976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime is disastrous for companies and can lead to major hits on a brand, reputation, an…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

733 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