Google Analytics

Search This Blog

Sunday, April 27, 2014

VB.Net How to Format Date String Not Based On Region Settings

This article applies to VB.NET (or C#.NET) based on .NET framework 3.5 SP1, compiled using Visual Studio 2008 SP1.

How do you format a DateTime into a string not based on the region settings (From Control Panels) ?

For instance, if you have a PC that has the following region settings:
  • Format: English (United States)
  • Short Date: dd-MMM-yy
  • Long time: h:mm:ss tt

This will result in 'DateTime.ToString' or 'Format' method to output the DateTime string based on those formats by default.

Nevertheless, sometimes different software may require customized formatting of DateTime based on specific requirements and needs.

Hence, if you would want to format a DateTime string as the following format:

dd/MM/yyyy HH:mm:ss


Here is what I found which works:

1.) Create a System.Globalization.DateTimeFormatInfo object based on a default CultureInfo.

2.) Explicitly reset the date separator as "/"

3.) Explicitly reset the time separator as ":"

4.) Invoke DateTime.ToString by specifying 'G' as 'format' parameter and the System.Globalization.DateTimeFormatInfo object as the 'provider' parameter.

Imports System.Globalization

Private Sub FormatDate()

        Try

            Dim dtfi As DateTimeFormatInfo = _ 
                        CultureInfo.CreateSpecificCulture("").DateTimeFormat
            Dim date1 As DateTime = Now 'set as the datetime

            Debug.Print("Original Date Time format:")
            Debug.Print("   {0}, {1}, {2}, {3}", dtfi.ShortDatePattern, _
                        dtfi.ShortTimePattern, _
                        dtfi.LongTimePattern, date1.ToString(dtfi))

            dtfi.DateSeparator = "/"
            dtfi.ShortDatePattern = "dd/MM/yyyy"
            dtfi.TimeSeparator = ":"
            dtfi.LongTimePattern = "HH:mm:ss"

            Debug.Print("Revised Date Time format:")
            Debug.Print("   {0}, {1}, {2},{3}", dtfi.ShortDatePattern, _
                        dtfi.ShortTimePattern, dtfi.LongTimePattern, _
                        date1.ToString("G", dtfi)) 'make use of long time pattern


        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try

    End Sub


Note:

1.) Setting 'System.Globalization.CultureInfo.InvariantCulture' as the 'provider' parameter is not applicable in this scenario.

'System.Globalization.CultureInfo.InvariantCulture' is useful for converting a DateTime string into DateTime object using 'DateTime.ParseExact'

2.) If you are setting the output of the formatted date time string into a maskedtextbox, remember to set the 'culture' property of maskedtextbox according, otherwise, it will also be based on formats from the region settings.

References:

http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx

http://msdn.microsoft.com/en-us/librar/system.globalization.datetimeformatinfo.shortdatepattern.aspx

http://msdn.microsoft.com/en-us/library/ee825488%28v=cs.20%29.aspx

1 comment:

Mike said...

Great Programming Tips ..
Ref - hire asp.net developer