Or change the .ToString() to match the pattern you want to display as.
Assuming that the date is in a readily interpretable format... for instance if you display January 7th, 2008 in a numeric format you get 1/7/2008 in the U.S. but in the rest of the world it's 7/1/2008. So really, without knowing what format the string is in to start with which we normally interpret using the System.Globalization.CultureInfo.CurrentCulture (At which point we've done as much as we can to try and automatically verify the input format, at which point as Plater points out, it's as easy as) there's no way to know if it is already in the correct format or not - as you might guess, interpretation of date strings opens the biggest possibility of error.
To input dates you can use a number of options. Once a date is imported into a date object, it is broken into Year, Month and Day properties, the biggest concern is making sure that the right piece of the string makes it into the right property. After this, spitting it out in the right format is a piece of cake:
- 'Grab the regional settings from the current computer
-
-
Dim oCulture = System.Globalization.CultureInfo.CurrentCulture
-
-
'Force the correct interpretation regardless of culture...
-
'by far the tidiest option, but you rarely get the luxury
-
'of being able to interpret a date so elegantly...
-
-
Dim MyDate As Date = New Date(2008, 1, 7)
-
-
'Or
-
-
'Parse a date string assuming that it's in the format defined
-
'in the 'regional settings on the computer that is running the
-
'software...
-
-
Dim MyDate As Date = Date.Parse("1/7/2008", oCulture)
-
-
'Or
-
-
'Parse a date string explicitly defining the format used to
-
'interpret it and store 'it in the format defined in the regional
-
'settings of the computer that 'is running the software...
-
-
Dim MyDate As Date = Date.ParseExact("1/7/2008", "dd/mm/yyyy", oCulture)
-
-
'Output date in required format
-
'Explicitly define the output format:
-
-
Console.Writeline(MyDate.ToString("dd/mm/yyyy"))
-
-
'Or
-
-
'Output using the format from regional settings
-
-
Console.Writeline(MyDate.ToString(oCulture))
Alternatively, if you want to input or output in a different culture's format but don't know what format they use, then you can use the NLS (National Language Support) Language identifiers to specify the output format which will automatically be interpreted by the system:
- Dim oCulture = New System.Globalization.CultureInfo(&H0809) 'UK English
-
'Or
-
'Dim oCulture = New System.Globalization.CultureInfo("en-GB") 'UK English
-
Console.Writeline(MyDate.ToString(oCulture))
The following reference may come in handy, it provides a list of the culture/language names and their corresponding identifiers:
http://msdn.microsoft.com/en-us/libr...ltureinfo.aspx