VB.net: Date without time

感情迁移 提交于 2019-12-29 05:50:48

问题


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.


回答1:


Either use one of the standard date and time format strings which only specifies the date (e.g. "D" or "d"), or a custom date and time format string which only uses the date parts (e.g. "yyyy/MM/dd").




回答2:


FormatDateTime(Now, DateFormat.ShortDate)




回答3:


I almost always use the standard formating ShortDateString, because I want the user to be in control of the actual output of the date.

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.




回答4:


Format datetime to short date string and convert back to date

CDate(YourDate.ToString("d"))

Or use short date string

CDate(YourDate.ToShortDateString)



回答5:


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.



来源:https://stackoverflow.com/questions/356972/vb-net-date-without-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!