all groups > c# > november 2007 >
You're in the

c#

group:

Need help with handling a time field


Need help with handling a time field news.microsoft.com
11/22/2007 10:56:48 PM
c#:
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

Re: Need help with handling a time field Liz
11/22/2007 11:40:27 PM

[quoted text, click to view]

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



AddThis Social Bookmark Button