简体   繁体   English

如何在没有时间的情况下从表中检索日期列?

[英]How to retrieve Date column from the table without Time?

I have table names Student with a column named OrderDate of type Date in MySQL.我有表名Student ,在 MySQL 中有一个名为OrderDate的列,类型为 Date。

在此处输入图像描述

My Web API Code is below我的 Web API 代码如下

public class Student
{
    public DateTime OrderDate { get; set; }
}

 string sql = "SELECT OrderDate FROM Student";
 using (IDbConnection conn= new MySqlConnection(connString))
 {
      return  connection.Query<Student>(sql)).ToList();
  }

The above code is retuning the order date as上面的代码将订单日期重新调整为

  "orderDate": "2019-10-30T00:00:00"

The expected date value is "2019-10-30"预计日期值为“2019-10-30”

I tried different approaches (updating the sql, change the DateTime to DateOnly etc.)我尝试了不同的方法(更新 sql,将 DateTime 更改为 DateOnly 等)

  SELECT DATE_FORMAT(OrderDate, '%Y-%m-%d') FROM Student;

Please let me know the how to get date in YYYY-MM-DD format with out the time part.请让我知道如何以 YYYY-MM-DD 格式获取不带时间部分的日期。

Try to add [DataType(DataType.Date)] in your student class.尝试在您的学生 class 中添加[DataType(DataType.Date)]

Example:例子:

public class Student
{
    [DataType(DataType.Date)]
    public DateTime OrderDate { get; set; }
}

See reference here .请参阅此处的参考。

After that run your app and see the output.之后运行您的应用程序并查看 output。

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

相关问题 如何在日期和时间之间从azure表存储中检索数据 - How to retrieve data from azure table storage between date and time 如何停止表格在日期栏中显示时间? - how do i stop the table from showing the time in the date column? 从数据库表中获取没有时间的日期 - Get date without time from DB table 如何使用Linq到实体从列中检索最大的日期和时间? - How can I retrieve the biggest date and time from the column using Linq to entity? 如何将日期时间选择器中的日期值转换为Visual C#中SQL Server表中的“日期”列? - How to convert the date value taken from the date time picker to the Date column in SQL Server table in Visual C#? 如何从日期列中排除时间 - How to exclude time from date column 如何在c#中从SQL检索日期和时间到datetimepicker? - How to Retrieve Date and time from SQL to datetimepicker in c#? 如何从DateTime字符串中检索日期和时间 - How do I retrieve the date and time from DateTime string 如何在不使用循环的情况下从数据集表中检索“特定列”。 VS 2005 C# - How do I retrieve Specific Column from dataset table without using loop. VS 2005 c# 如何从MS SQL数据表中检索列默认值 - How to retrieve column default value from MS SQL data table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM