Formatting of Date and Time in .NET

I don't know about you, and perhaps this is just an issue in non-english speaking countries, but the formatting of date and time has always been an issue in our old applications built with ASP and COM components. Especially VB6 components. Some developers settled with setting the default region of the server OS to whatever language the web application was supposed to be read in and then used VB functions such as Now, Time and Date to get and display the date or time formatted according to the regional settings. Well, this worked well until you had to move the application to another box or when some administrator or other changed the default regional settings to something else.

The solution to this was to build or format the date and time strings yourself, either by concatenating or by using the built-in Format method. But now the Framework library contains a number of powerful features, which help the application developer present date and time strings in the correct format for the user. This is called Globalization, and achieved by setting the specific “culture” of the running process or the current thread. There is no longer need to build date and time strings manually, and I recommend web developers to use one of the following methods for setting the correct culture in their ASP.NET applications:

  1. Set the culture in Web.Config
  2. Set the culture of the current request thread
  3. Set the culture parameter of the ToString method

I guess you can also set the culture of a Console or WinForm application in the application configuration file just as in the web.config, but I've not tried that yet.

I always keep date and time data in a Date or a DateTime structure. The Date class is only available in VB.NET but the compiler will translate that into a DateTime structure. The DateTime contains a few convenient methods for formatting date and time strings, which I use most of the time:

  • ToShortDateString
  • ToShortTimeString
  • ToLongDateString
  • ToLongTimeString

The value of the DateTime instance is formatted using different format characters and passed through the ToString method. As the .NET documentation for DateTime says, the return value for the ToShortDateString is identical to the value returned by ToString ("d", null).

If a more fine-grained control is needed, you can use the overloaded ToString(format) methods or the static Format method from the Microsoft.VisualBasic namespace, which should be familiar to VB6 developers. I still prefer the different ToXxxxString methods provided by the DateTime class.

To get the current date and time in VB.NET, you can use either the .NET DateTime or the Date class, as I wrote above. Or, you can use the Now property, which is part of the DateAndTime module from the Microsoft.VisualBasic namespace and also returns a Date object containing the current date and time. Something like this:


        Dim Date1 As String = DateTime.Now.ToShortDateString()
        Dim Date2 As String = Now.ToShortDateString()
        Dim Date3 As String = Date.Now.ToShortDateString()
 
To get the current date and time in C#, you must use the DateTime structure, since the Date class doesn't exist: 


       string Date1 = DateTime.Now.ToShortDateString();
       DateTime dt1 = DateTime.Now;
       string Date2 = dt1.ToShortDateString();

I guess you could import the Microsoft.VisualBasic namespace and use the Now function from it, but hey... :)

Have to rush home now, so I'll post some samples of how to set the culture tomorrow or so.

3 Comments

Comments have been disabled for this content.