C# 为什么date.ToString()会随时间显示

C# 为什么date.ToString()会随时间显示,c#,sql,sql-server,winforms,C#,Sql,Sql Server,Winforms,在我的sql server数据库中有一个带有日期数据类型的列,我将其插入其中1999-12-23。当我在数据库中运行select查询时,显示日期为1999-12-23,但当我将数据库连接到我的c winform应用程序并检索日期时,显示日期为1999-12-23 00:00:00,即显示日期和时间 这些是我使用的代码 创建表 CREATE TABLE Users.Personal ( /*...Other Codes for the table this is the main problem*

在我的sql server数据库中有一个带有日期数据类型的列,我将其插入其中1999-12-23。当我在数据库中运行select查询时,显示日期为1999-12-23,但当我将数据库连接到我的c winform应用程序并检索日期时,显示日期为1999-12-23 00:00:00,即显示日期和时间

这些是我使用的代码

创建表

CREATE TABLE Users.Personal
(
/*...Other Codes for the table this is the main problem*/
DateofReg date NOT NULL
)
选择查询

SELECT * FROM Users.Personal
这将显示日期为1999-12-23

与数据库的连接

private void RetrievePersonalDetails()
{
    SqlConnection myConnection = new SqlConnection("server=AMESINLOLA;" +
        "Trusted_Connection=yes;" +
        "database=Crm_Db;");
    myConnection.Open();

    SqlCommand myCommand = myConnection.CreateCommand();
    myCommand.CommandText = "SELECT * FROM Users.Personal WHERE UniqueID='" + uniqueid + "'";
    myCommand.CommandType = CommandType.Text;

    SqlDataReader myReader = myCommand.ExecuteReader();

    if (myReader.Read())
    {
        //Other codes inserting to textbox but this is the main problem
        txtDor.Text = myReader["DateofReg"].ToString();
    }
    else
    {
        MessageBox.Show("Empty");
    }
    myConnection.Close();
    myReader.Close();
}
这将显示日期为1999-12-23 00:00:00

为什么日期在应用程序中随时间显示,但在数据库中显示得很好?我可以做些什么来只显示日期?

myReader[DateofRef]似乎返回一个DateTime对象。它在内部存储日期值的刻度,包括时间和毫秒等

ToString为DateTime对象应用默认格式

 txtDor.Text = myReader["DateofReg"].GetDateTime.Date.ToString("d"));
你也可以使用 DateTime.Now.ToShortDateString,只打印年、月和日

虽然格式将取决于当前区域性Thread.CurrentThread.CurrentCulture,但ToString还接受一个名为IFormatProvider的参数,该参数可以设置为任何区域性,以便使用CultureInfo指定日期字符串的外观

您可以通过将格式传递给ToString方法来更改格式

在这里可以找到许多示例

第一种解决方案:

txtDor.Text = myReader["DateofReg"].ToShortDateString();
第二个,我不推荐:

txtDor.Text = myReader["DateofReg"].ToString().Substring(0,10);

尝试ToSortDateString而不仅仅是ToString
虽然SQL Server有一个日期类型,它是一个没有时间的日期,.NET在核心基类库中没有类似的内容。所以它使用日期时间,而时间设置为午夜

有很多方法可以从DateTime获取只包含日期的字符串,但由于myReader[DateofReg]将该日期时间装箱为对象,因此如果要对其执行任何操作,需要首先强制转换该日期时间。比如说,

// Unbox the result by casting
DateTime dt = (DateTime) myReader["DateofReg"];

// Use a string formatter to get what you want
txtDor.Text = dt.ToString("d");

// or if you prefer, use this shortcut method
txtDor.Text = dt.ToShortDateString();

这应该可以很好地工作,但是如果出于某种原因,您实际上想要一个没有时间类型的纯日期,而不仅仅是字符串或DateTime,那么您可以使用库中的LocalDate类型。

这将确保返回的代码符合短DateTime格式的DateTime对象

 txtDor.Text = myReader["DateofReg"].GetDateTime.Date.ToString("d"));

例如,此代码将以dd/MMM/yyyy格式返回日期。例如,2013年10月9日将采用2013年10月9日的格式

txtDor.Text = myReader["DateofReg"].ToString("dd/MMM/yyyy");

URL似乎指向Deutsche msdn。@Ela我不知道如何apply@PreciousTijesunimi您的代码行,只需替换myReader[DateofReg].ToString;使用DateTimemyReader[DateofReg].ToSortDateString;例如,我会做的。当然,这不是很干净的代码,在格式化之前可能会强制转换它it@ElaToSortDateString不显示在IntelisSense中,您必须首先将其强制转换为DateTime对象。var date=myReader[DateofReg]作为DateTime。orDateTimemyReader[DateofReg].ToSortDateString;应该可以,但GetDateTime周围会出现错误