Solved

Set the default regional date format in vb.net

Posted on 2004-09-05
2
1,276 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 125 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now