?
Solved

Set the default regional date format in vb.net

Posted on 2004-09-05
2
Medium Priority
?
1,726 Views
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?

John.
0
Comment
Question by:cobrajohn123
2 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 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")

-Baan
0
 
LVL 10

Expert Comment

by:123654789987
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")
      PrintPattern("en-US")
      PrintPattern("ja-JP")
      PrintPattern("fr-FR")

   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.
'
' CULTURE    PROPERTY VALUE
'  en-US     M/d/yyyy
'  ja-JP     yyyy/MM/dd
'  fr-FR     dd/MM/yyyy
'


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 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