locked
How to RETURN the date format string from a Locale (if possible)? RRS feed

  • Question

  • User1090916182 posted

    Hi

    Given a locale, say, "en-gb" or "en-us" I wondered if it is possible to read back the date format specifier for each one?

    For example, if "en-gb" then the specifier would return "dd/mm/yyyy"

    For "en-us" it would return "mm/dd/yyyy"

    Is that possible to do at all please?  This is so that I can show this as a hint next to the date fields in my application.

    Wednesday, April 27, 2011 11:49 AM

Answers

All replies

  • User-610330605 posted

    Try this

    using System.Globalization;

    string currentDateFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;

    http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.shortdatepattern.aspx

    Wednesday, April 27, 2011 12:28 PM
  • User1090916182 posted

    Hi

    Thanks for that.  The results however aren't quite what I was expecting.  I wondered if you could answer my next question...

    Within Web.Config I have defined the following:

    <globalization uiCulture="en" culture="en-GB" responseEncoding="utf-8" requestEncoding="utf-8" />

    and my browser is also configured to use en-GB as the default Locale

    However, in my ASP.NET, when I try this:

    	Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    		Trace.Warn(System.Globalization.CultureInfo.CurrentCulture.Name)
    		Trace.Warn(System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern)
    		Trace.Warn(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern)
    	End Sub

    then the output shows:

    en-GB ' correct
    M/d/yyyy
    dd/MM/yyyy ' correct
    Could you please explain how CurrentCulture differs from CurrentUiCulture, and why CurrentUiCulture would show M/d/yyy which is not correct?  Also, why is the MM part in capital letters in the CurrentCulture ShortDataPattern???

    Thursday, April 28, 2011 4:44 AM
  • User-610330605 posted

    Could you please explain how CurrentCulture differs from CurrentUiCulture

    CurrentCulture -  the culture used by the current thread. (for formatting date, time etc)
    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=VS.71).aspx

    CurrentUiCulture - the current culture used by the Resource Manager to look up culture-specific resources at run time.
    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentuiculture(v=vs.71).aspx

    why CurrentUiCulture would show M/d/yyy which is not correct?

    Why is it not correct? it shows a different value from CurrentCulture because you set the neutral value instead of the country specific value.
    http://msdn.microsoft.com/en-us/library/441487wx.aspx

    why is the MM part in capital letters in the CurrentCulture ShortDataPattern???

    MM stands for month from 01 through 12 and mm stands for minutes from 00 through 59.
    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2011 5:28 AM
  • User1090916182 posted

    Thanks for your great explanations.

    Just to clarify, in web.config if I set uiCulture to be "en-GB" instead of "en" that would stop M/d/yyyy appearing in System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern , right?

    Thanks again.

    Thursday, April 28, 2011 3:53 PM
  • User-610330605 posted

    Just to clarify, in web.config if I set uiCulture to be "en-GB" instead of "en" that would stop M/d/yyyy appearing in System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern , right?

    Yes it should.

    Thursday, April 28, 2011 7:23 PM