views:

39

answers:

4

Hi, 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?

+1  A: 
cmdItemSearch.Parameters["@EndDate"].Value = DateTime.Today;

Note that the Today property simply returns a DateTime with the time element set to midnight.

LukeH
DateTime.Now.Date is the same as DateTime.Today
Robin Day
@Robin: Was just making that change when you commented!
LukeH
A: 

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"));
Josh
+2  A: 

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.

Robin Day
when I hover over the word "value", I see "6/30/2010 12:00:00 AM"How can I be sure it is passing the date only, and not the time as well?
DotNetRookie
Well, your StoredProc is passing a Sql DateTime which will always have a time as well anyway. Your stored procedure will need to deal with this.If you have SQL Server 2008 then you can just use the Date datatype instead.
Robin Day
I have SQL 2005
DotNetRookie
@DotNetRookie: You can't, .NET doesn't have a date-only type. The convention for representing date-only is setting the time component to midnight.
LukeH
Got it. Thanks for the info, LukeH
DotNetRookie
+1  A: 

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

phsr
That returns a `string`, not a `DateTime`.
LukeH
True, but I figured that someone may be looking to get just the short date string, so I included it
phsr