hello sir
how can I get my system short date format in a label in vb. net. I try this code :

Label1.Text = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern()

but show wrong data. all time show "M/d/yyyy" format.

I change it(to "dd/MM/yyyy") manually and by coding result is same("M/d/yyyy") please help me.....

For Change the Format:

Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\International", "sShortDate", "dd/MM/yyyy")
ddanbe commented: Thanks for marking this solved +15

Dani AI

Generated

You are reading and displaying the pattern, not a formatted date, and you are using CurrentUICulture. UI culture is for resource lookup; date formatting uses CurrentCulture. To show the user’s short date, format an actual DateTime with the standard "d" specifier and the current culture.

Imports System.Globalization

Dim dt = DateTime.Now
Label1.Text = dt.ToString("d", CultureInfo.CurrentCulture)

In ASP.NET, remember that "system" means the server’s culture. If you want each visitor’s browser culture, enable auto-detection so the request thread’s CurrentCulture is set for you. Then the same ToString("d") will honor that culture.

<!-- web.config -->
<configuration>
  <system.web>
    <globalization culture="auto" uiCulture="auto" />
  </system.web>
</configuration>

Avoid writing to the registry in a web app; it affects the server account, not the client, and is unnecessary once culture is configured. For storage (DB, APIs), keep dates in an invariant, unambiguous format, and only format for display at the UI boundary. See Standard date and time format strings and the ASP.NET for details.

Recommended Answers

All 3 Replies

Well to set the Text of your label, I would use the ToString method of DateTime with a format string. See here for examples.

Take a look here

                Dim dt As DateTime = Now
                Dim sDate As String = dt.ToString("yyyy-MM-dd")

Or you can change current's thread date pattern:

                Dim newCulture As System.Globalization.CultureInfo =
                    System.Threading.Thread.CurrentThread.CurrentCulture.Clone()
                newCulture.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"
                newCulture.DateTimeFormat.DateSeparator = "-"
                System.Threading.Thread.CurrentThread.CurrentCulture = newCulture
                Dim sDate As String = Now.ToString
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.