Cannot return a short date?

CodeLiftsleep

Member
Joined
Mar 11, 2016
Messages
20
Programming Experience
3-5
I have tried every method you can to do it, it still is always returning the time along with it.

I have tried Date.toshortdatestring()

I have tried Date.tostring("d")

Nothing works...still getting the time afterwards...I know I can just remove it but I am just curious as to why the method is in there when it doesn't work?
 
What you're trying to do is not possible, but that's not a problem because you should be trying to do something else anyway.

There is no type that represents a date without a time in VB.NET. Despite the name, the native VB Date type doesn't represent just a date. All native VB types are implemented using .NET types and its the .NET DateTime structure that is used to implement the VB Date type. As that name suggests, it represents a date and a time. If a DateTime value is intended to be used to represent just a date then the time portion is zeroed to midnight.

There is also no concept of a short date as far as values are concerned. A DateTime simply represents a moment in time. It contains a number that is an offset from a known origin. It is neither short nor long nor in between. It just is. Things like short and long are matter of format, i.e. how a date is represented when it's displayed. "January 1, 2000" and "01/01/2000" are two different representations but they both represent the same date.

So, when dealing with dates in your code you use the VB Date or the .NET DateTime type. Both are the same thing so it doesn't matter which but you should be consistent. If you use other VB types, e.g. Integer and Long, then you should use Date and if you use other .NET types, e.g. Int32 and Int64, then you should use DateTime. Those values will do everything and anything you need to do relating to dates.

When it comes time to display a date value to the user, that is when format becomes an issue. At that point, if you want to display a short date to the user then you call ToShortDateString on your DateTime value and that will return a String that you can then display in a Label or wherever else. Note that it is a String, not a DateTime that you are displaying. Format is only an issue for display.
 
Back
Top