hi..i want to know the way to extract date and time separately from the currently selected value in datetime picker control??
views:
524answers:
2DateTimePicker.Value
returns a DateTime
, so you can use the Date
and TimeOfDay
properties of DateTime.
Important Note:
If you're saving the value to a database, put the date value in a data parameter, don't concatenate strings to build SQL queries; that'll leave you open to the SQL injection attack. Here's a brief example (not tested code, just to give you an idea):
DateTime theDate = dtPicker.Value.Date;
IDbCommand command = GetDbCommand("insert into table_name (name, thedate) values (@name, @thedate)");
command.Parameters.Add(command.CreateParameter("@name", theName));
command.Parameters.Add(command.CreateParameter("@thedate", theDate));
command.ExecuteNonQuery();
More info on avoiding SQL injection attacks (and solving your "invalid month") problem can be found here:
http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx
if you just want them for display purposes, you can to the following:
DateTimePicker.Value.ToShortDateString() - will give you a date string
DateTimePicker.Value.ToShortTimeSttring() - will give you time string