VB.net:没有时间的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/356972/
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
VB.net: Date without time
提问by Hyman
How do you format the date time to just date?
你如何将日期时间格式化为日期?
For example, this is what I retrieved from the database: 12/31/2008 12:00:00 AM, but I just want to show the date and no time.
例如,这是我从数据库中检索到的:12/31/2008 12:00:00 AM,但我只想显示日期而不显示时间。
回答by Jon Skeet
Either use one of the standard date and time format stringswhich only specifies the date (e.g. "D" or "d"), or a custom date and time format stringwhich only uses the date parts (e.g. "yyyy/MM/dd").
使用仅指定日期的标准日期和时间格式字符串之一(例如“D”或“d”),或仅使用日期部分的自定义日期和时间格式字符串(例如“yyyy/MM/dd”) )。
回答by madcolor
FormatDateTime(Now, DateFormat.ShortDate)
FormatDateTime(现在,DateFormat.ShortDate)
回答by Rick
I almost always use the standard formating ShortDateString, because I want the user to be in control of the actual output of the date.
我几乎总是使用标准格式的 ShortDateString,因为我希望用户能够控制日期的实际输出。
Code
代码
Dim d As DateTime = Now
Debug.WriteLine(d.ToLongDateString)
Debug.WriteLine(d.ToShortDateString)
Debug.WriteLine(d.ToString("d"))
Debug.WriteLine(d.ToString("yyyy-MM-dd"))
Results
结果
Wednesday, December 10, 2008
12/10/2008
12/10/2008
2008-12-10
Note that these results will vary depending on the culture settings on your computer.
请注意,这些结果会因计算机上的区域性设置而异。
回答by Tony Dong
Format datetime to short date string and convert back to date
将日期时间格式化为短日期字符串并转换回日期
CDate(YourDate.ToString("d"))
Or use short date string
或使用短日期字符串
CDate(YourDate.ToShortDateString)
回答by Brian Knoblauch
Or, if for some reason you don't like any of the more sensible answers, just discard everything to the right of (and including) the space.
或者,如果由于某种原因您不喜欢任何更明智的答案,只需丢弃(包括)空间右侧的所有内容。