Solved

Set the default regional date format in vb.net

Posted on 2004-09-05
2
1,461 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 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")

-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

Technology Partners: 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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