[vb.net] VB.net: Date without time

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.

This question is related to vb.net formatting

The answer is


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.


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.


FormatDateTime(Now, DateFormat.ShortDate)


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.


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.


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.


Format datetime to short date string and convert back to date

CDate(YourDate.ToString("d"))

Or use short date string

CDate(YourDate.ToShortDateString)

FormatDateTime(Now, DateFormat.ShortDate)


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.


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.


Format datetime to short date string and convert back to date

CDate(YourDate.ToString("d"))

Or use short date string

CDate(YourDate.ToShortDateString)

FormatDateTime(Now, DateFormat.ShortDate)


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.


FormatDateTime(Now, DateFormat.ShortDate)