如何从日期时间中删除时间? [英] How to remove time from datetime ?
问题描述
在我的数据库中,我有数据类型日期列。
来自c#I的
我这样做,其中也包含时间..
Hi ,
In my database I have column of datatype date.
from c# I am doing this , which contains time as well ..
DateTime date = new DateTime(fYear, month, 1)
任何可以消除时间并仅存储数据的方式到sql db?
我尝试过:
日期时间日期= new DateTime(fYear,month,1)。日期;
any way which will removes time and store only dat to sql db ?
What I have tried:
DateTime date = new DateTime(fYear, month, 1).Date;
推荐答案
根据您使用的数据库类型,您需要将C#中的DateTime转换为字符串。
通常日期值由#括起来如下;
Depending on what type of Database you are using you will need to convert the DateTime in C# to a string.
Typically Date values are surrounded by #'s as below;
#1/1/2000#
最简单的方法是使用
The easiest way is to therefore convert the date to a string using
DateTime.ToString("d/M/yyyy");
查看以下MSDN文章;
MSDN - DateTime.ToString(string)[ ^ ]
MSDN - 标准日期和时间格式字符串[ ^ ]
MSDN - 自定义日期和时间格式字符串[ ^ ]
Take a look at the following MSDN articles;
MSDN - DateTime.ToString(string)[^]
MSDN - Standard Date and TIme Format Strings[^]
MSDN - Custom Date and Time Format Strings[^]
DateTime db = new DateTime(2016,12,3);
Console.WriteLine(db.ToString("yyyy-MM-dd"));
result: 2016-12-03
这篇关于如何从日期时间中删除时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!