[英]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; }
}
After that run your app and see the output.之后运行您的应用程序并查看 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.