I have a parameter string that 开发者_开发问答passes date value to a stored proc
cmdItemSearch.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
cmdItemSearch.Parameters["@EndDate"].Value = Convert.ToDateTime(DateTime.Now);
The value being passed is "6/30/2010 7:45:00 AM"
I want to pass only "6/30/2010" How would I do that?
For starters, DateTime.Now
is already a DateTime
so doesn't need to be converted as you have.
Secondly, you can obtain just the date of Today by using DateTime.Today
instead of DateTime.Now
.
However, if your date isn't "today" then you can just use yourDateTime.Date
to return just the Date.
If you are looking for the mm/dd/yyyy format, you could use
DateTime.Now.ToShortDateString()
That will return the short format, but depends on the current culture
cmdItemSearch.Parameters["@EndDate"].Value = DateTime.Today;
Note that the Today
property simply returns a DateTime
with the time element set to midnight.
MSDN to the rescue: http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
Create a variable called EndDate
var EndDate = DateTime.Now.ToString("MM/dd/yyyy");
EndDate = Convert.ToDateTime(EndDate);
Now EndDate Type is DateTime;
you pass it as a parameter
cmdItemSearch.Parameters["@EndDate"].Value = EndDate ;
精彩评论