Need help with handling a time field

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

I have a form field that accepts a start time and one that accepts an end
time. I am using a masked editor that allows the user to enter the hour and
minutes and am and pm. When I save the form how do I add the current date
to the time to be able to save it to the sql server table? I really don't
care about the date itself just the times. Or should I save the times to a
varchar and just convert back and forth as needed?


Bill
 
I have a form field that accepts a start time and one that accepts an end
time. I am using a masked editor that allows the user to enter the hour
and minutes and am and pm. When I save the form how do I add the current
date to the time to be able to save it to the sql server table? I really
don't care about the date itself just the times. Or should I save the
times to a varchar and just convert back and forth as needed?

probably a thousand ways to get where you're going; here's one:

string t1 = DateTime.Now.ToShortDateString() + " " +
MaskedTextBoxStartTime.Text;

string sql = "INSERT tbl (timeField, ...) VALUES ('" + t1 + "')" ....

you haven't said anything about what you're doing with these times but I'll
assume you want to make calculations of some sort on them; personally, I
would probably store them as DateTime or SmallDateTime types and avoid
varchar (or char since they're not really variable) since there is so much
support for date and time calculation built in to the .NET Framework and in
SQL Server ... if you have 500 million rows maybe you need/want to type
them otherwise

you need not limit yourself to char/varchar for SQL storage; if you feel
like using a numeric type you could .. you could store the number of minutes
since midnight as Int16/smallint, for example (10:15 AM = 615) (which is
what SQL Server does internally with the 2 byte time segment of
SmallDateTime anyway) ... obviously you need routines to convert these back
and forth; can't beat this for efficiency .. but I wouldn't get caught up in
efficiency unless you have a LOT of these start/end pairs ... chances are
your performance differentials will be trivial so ... make it easy on
yourself is the likely best bet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top