Set the default regional date format in

Posted on 2004-09-05
Last Modified: 2008-02-01
Hi there,

I've just started porting an old ASP application to VB.NET and I'm having problems with the good old regional date format issue I've encountered in ASP before.

Even thougth the regional date format on the server is set to UK format (i.e. "dd/mm/yyyy"), all dates are being reported back as US format ("mm/dd/yyyy"). This is a problem because the VB.NET code is passing arrays of dates back to the ASP and it is going to cause big difficulties if I can't set some sort of default format up.

I have tried playing about with the Format command, but that doesn't always seem to work... I also found the following function on a Microsoft website:

    Function FormatDate(ByVal culture As String, ByVal thedate As String, Optional ByVal outputformat As String = "OtherDateTime") As String

       If IsDate(thedate) Then

            Dim thisDate As Date = CDate(thedate)
            Dim ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(culture, False)
            Dim dt As System.Globalization.DateTimeFormatInfo = ci.DateTimeFormat

            Select Case outputformat
                Case "LongDatePattern"
                    Return thisDate.ToString(dt.LongDatePattern)
                Case "LongTime"
                    Return thisDate.ToString(dt.LongTimePattern)
                Case "ShortDate"
                    Return thisDate.ToString(dt.ShortDatePattern)
                Case "ShortTime"
                    Return thisDate.ToString(dt.ShortTimePattern)
                Case "UniversalSortableDateTime"
                    Return thisDate.ToString(dt.UniversalSortableDateTimePattern)
                Case "SortableDateTime"
                    Return thisDate.ToString(dt.SortableDateTimePattern)
                Case "FullDateTime"
                    Return thisDate.ToString(dt.FullDateTimePattern)
                Case Else
                    Return thisDate.ToString(dt.FullDateTimePattern)
            End Select
        End If
 end function

I am passing "en-gb" in as the regional code, but returned values are unpredictable.

How do I force the UK date format to be used for everything?

Question by:cobrajohn123
LVL 18

Accepted Solution

DotNetLover_Baan earned 125 total points
ID: 11984877
Hi there,
use Globalization and then change the current thread to set the culture info..

Imports System.Globalization
Imports System.Threading

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-gb")  'Make it whatever you want
TextBox1.Text = Now.ToString("dd/mm/yyyy hh:mm tt")

LVL 10

Expert Comment

ID: 11987140
U can add the culture information to a date in this way

Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")

' Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"

' Use the DateTimeFormat from the culture associated
' with the current thread.

Console.WriteLine( dt.ToString("d") )  
Console.WriteLine( dt.ToString("m") )

' Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )    ' Here d represents the short date pattern

' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) ) ' Here m represents the month-day pattern

' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )

Second example

The following code example displays the value of ShortDatePattern for a few cultures.
[Visual Basic]
Imports System
Imports System.Globalization

Public Class SamplesDTFI

   Public Shared Sub Main()

      ' Displays the values of the pattern properties.
      Console.WriteLine(" CULTURE    PROPERTY VALUE")

   End Sub 'Main

   Public Shared Sub PrintPattern(myCulture As [String])

      Dim myDTFI As DateTimeFormatInfo = New CultureInfo(myCulture, False).DateTimeFormat
      Console.WriteLine("  {0}     {1}", myCulture, myDTFI.ShortDatePattern)

   End Sub 'PrintPattern

End Class 'SamplesDTFI

'This code produces the following output.
'  en-US     M/d/yyyy
'  ja-JP     yyyy/MM/dd
'  fr-FR     dd/MM/yyyy


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question