vb.net 如何从 DateTime 值中删除时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17690859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove the Time from a DateTime value?
提问by Sabilv
In my SQL database, I have a column formatted as DateTimeand when I retrieve data from that column in ASP.NET, I catch it on the Date variable, than pass the value to textbox:
在我的 SQL 数据库中,我有一列格式为DateTime,当我在 ASP.NET 中从该列检索数据时,我在 Date 变量上捕获它,而不是将值传递给文本框:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.ToString
but when I debug my website, the txtSchedDate.Textstill gives me the full DateTimevalue:
但是当我调试我的网站时,它txtSchedDate.Text仍然给了我全部的DateTime价值:
7/17/2013 12:00:00 AM
is it possible to eliminate the time value here and just return the date?
是否可以消除这里的时间值并只返回日期?
回答by Adriaan Stander
Have you tried using something like
你有没有试过使用类似的东西
txtSchedDate.Text = Y.Date.ToString("MM/dd/yyyy")
or which ever format you wish to display.
或您希望显示的任何格式。
Have a look at
看一下
DateTime.ToString Method (String)
Converts the value of the current DateTime object to its equivalent string representation using the specified format.
使用指定的格式将当前 DateTime 对象的值转换为其等效的字符串表示形式。
Custom Date and Time Format Strings
回答by Alvin
Convert.ToDateTime(dt.Rows(0)("SCH_DATE")).ToString("M/d/yyy")
回答by yaoxing
Besides answers above, you can try converting it in SQL server
除了上面的答案,您还可以尝试在 SQL Server 中进行转换
SELECT CONVERT(varchar(15), GETDATE(), 11)
Keep in mind after converting it's VARCHAR(15)instead of DATETIME.
转换后记住它是VARCHAR(15)而不是DATETIME.
回答by Damith
you can get date by txtSchedDate.Text = Y.Date.ToShortDateString()
你可以通过 txtSchedDate.Text = Y.Date.ToShortDateString()
回答by Karl Anderson
Once you have a Dateobject, you can get the constituent pieces if you wish as well, like this:
一旦你有了一个Date对象,如果你愿意,你也可以得到组成部分,就像这样:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.Year & "-" & Y.Date.Month & "-" & Y.Date.Day
Or you can use the custom and standard date and time format strings mentioned by others.
或者您可以使用其他人提到的自定义和标准日期和时间格式字符串。

