?
Solved

Set the default regional date format in vb.net

Posted on 2004-09-05
2
Medium Priority
?
1,577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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