简体   繁体   English

仅在asp.net中的Convert.ToDateTime()中获取日期

[英]Getting the date only in Convert.ToDateTime() in asp.net

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" 传递的值为“ 6/30/2010 7:45:00 AM”

I want to pass only "6/30/2010" How would I do that? 我只想通过“ 2010年6月30日”,该怎么办?

For starters, DateTime.Now is already a DateTime so doesn't need to be converted as you have. 对于初学者来说, DateTime.Now已经是一个DateTime因此不需要像您一样进行转换。

Secondly, you can obtain just the date of Today by using DateTime.Today instead of DateTime.Now . 其次,您可以使用DateTime.Today而不是DateTime.Now获得今天的DateTime.Now

However, if your date isn't "today" then you can just use yourDateTime.Date to return just the Date. 但是,如果您的日期不是“今天”,则可以使用yourDateTime.Date仅返回日期。

If you are looking for the mm/dd/yyyy format, you could use 如果您要查找mm / dd / yyyy格式,则可以使用

DateTime.Now.ToShortDateString()

That will return the short format, but depends on the current culture 那将返回短格式,但取决于当前的文化

cmdItemSearch.Parameters["@EndDate"].Value = DateTime.Today;

请注意, Today属性仅返回一个DateTime ,并将time元素设置为午夜。

MSDN to the rescue: http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx 抢救MSDN: 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 创建一个名为EndDate的变量

var EndDate = DateTime.Now.ToString("MM/dd/yyyy"); var EndDate = DateTime.Now.ToString(“ MM / dd / yyyy”);

EndDate = Convert.ToDateTime(EndDate); EndDate = Convert.ToDateTime(EndDate);

Now EndDate Type is DateTime; 现在EndDate类型为DateTime;

you pass it as a parameter 您将其作为参数传递

cmdItemSearch.Parameters["@EndDate"].Value = EndDate ; cmdItemSearch.Parameters [“ @ EndDate”]。Value = EndDate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM