locked
Displaying Date in Windows Store apps regarding Language. RRS feed

  • Question

  •         Hi,
                In my Windows Store application I am displaying Date as Format(Sunday, March 09, 2013) Using "{0:D}" 
    I think Windows Default Language is English (United Kingdom). In my app I want to display multi languages. If I Change the language, Suppose i changed to English (United States) the date is displaying as (Sunday Sunday, March 09, 2013).

    This is my Sample code:
    loadedDate = DateTime.ParseExact(data.issues[four].date.ToString(), "yyyyMMdd", null);
    var date = loadedDate.DayOfWeek + " " + String.Format("{0:D}", loadedDate);
    txt4.Text = date;
    If i try to change to any language it is displaying in same manner:
    Any help it would be Helpful.
    Thanks.
    Monday, April 29, 2013 5:41 AM

Answers

  • Can you make some samples what would you like to get when user has english as default language and then for german?

    It's easy to do formatting if you have only in english like:

    DateTime.Now.ToString("D", CultureInfo.Invariant);

    Or if you want to have same date format but in local language:

    DateTime.Now.ToString("dddd, dd MMM, YYYY", new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]));

    But I believe you want to achieve this: (a combination of both)

    CultureInfo culture = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);
    
    DateTime date = DateTime.Now;
    
    // Get localized day name (Monday or in Slovenian ponedeljek)
    string dayName = date.ToString("dddd", culture);
    
    // Get localized month with day number (15 June or in Slovenian 15. junij)
    string monthPattern = date.ToString("m", culture);
    
    // Get localized date in desired pattern (Monday, 15 June, 2013 or in Slovenian ponedeljek, 15. Junij, 2013)
    string result = string.Format("{0}, {1}, {2}", dayName, monthPattern, date.Year);

    I think there is no better way to make this if you want to keep localized date format.

    Results in different languages:

    English: "Monday, 29 April, 2013"
    Slovenian: "ponedeljek, 29. april, 2013"
    German: "Montag, 29. April, 2013"



    • Edited by Jernej Kavka Monday, April 29, 2013 1:21 PM
    • Proposed as answer by Jernej Kavka Monday, April 29, 2013 1:21 PM
    • Marked as answer by Aaron Xue Wednesday, May 8, 2013 10:08 AM
    Monday, April 29, 2013 1:12 PM
  • You can use something like this:

    DateTime.Now.ToString("D", new CultureInfo(Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride));

    Or this if you want to use users primary language date time format:

    DateTime.Now.ToString("D", new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]));

    • Proposed as answer by RishiRajIT Monday, April 29, 2013 12:38 PM
    • Marked as answer by Aaron Xue Wednesday, May 8, 2013 10:08 AM
    Monday, April 29, 2013 8:04 AM
  • The DateTimeFormatter class is used to format date and times. This is the reason why the sample (and its documentation) were referred to. That class uses 'patterns' and 'structures' you'll need to get acquainted with in order to achieve your goal. F.i. in the  "OverrideSettings" UserControl [adapt to your busines cases]:
    new DateTimeFormatter(YearFormat.Abbreviated,
                        MonthFormat.Abbreviated,
                        DayFormat.Default,
                        DayOfWeekFormat.Full,
                        HourFormat.None,
                        MinuteFormat.None,
                        SecondFormat.None,
                        new[] { "de-DE" },
                        "DE",
                        CalendarIdentifiers.Gregorian,
                        ClockIdentifiers.TwelveHour),

    • Marked as answer by Aaron Xue Wednesday, May 8, 2013 10:08 AM
    Monday, April 29, 2013 1:13 PM

All replies

  • The Date and time formatting sample contains valuable material as well as links to the documentation.
    Monday, April 29, 2013 7:04 AM
  • You can use something like this:

    DateTime.Now.ToString("D", new CultureInfo(Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride));

    Or this if you want to use users primary language date time format:

    DateTime.Now.ToString("D", new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]));

    • Proposed as answer by RishiRajIT Monday, April 29, 2013 12:38 PM
    • Marked as answer by Aaron Xue Wednesday, May 8, 2013 10:08 AM
    Monday, April 29, 2013 8:04 AM
  • @ForInfo, @Jernej Kavka

    Thank you for your answer. But what the issue is I am getting the value of DayofWeek in default language. If I change my language to any other language it is displaying with both DayofWeek names "Sunday(default language DayofWeek) Freitag (German DayofWeek) ,26.April 2013"
    I want to display it as Normal date format of 
    "{0:D}" when I change my language also 
    I hope you understand my issue.
     

    devendra

    Monday, April 29, 2013 12:47 PM
  • Can you make some samples what would you like to get when user has english as default language and then for german?

    It's easy to do formatting if you have only in english like:

    DateTime.Now.ToString("D", CultureInfo.Invariant);

    Or if you want to have same date format but in local language:

    DateTime.Now.ToString("dddd, dd MMM, YYYY", new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]));

    But I believe you want to achieve this: (a combination of both)

    CultureInfo culture = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);
    
    DateTime date = DateTime.Now;
    
    // Get localized day name (Monday or in Slovenian ponedeljek)
    string dayName = date.ToString("dddd", culture);
    
    // Get localized month with day number (15 June or in Slovenian 15. junij)
    string monthPattern = date.ToString("m", culture);
    
    // Get localized date in desired pattern (Monday, 15 June, 2013 or in Slovenian ponedeljek, 15. Junij, 2013)
    string result = string.Format("{0}, {1}, {2}", dayName, monthPattern, date.Year);

    I think there is no better way to make this if you want to keep localized date format.

    Results in different languages:

    English: "Monday, 29 April, 2013"
    Slovenian: "ponedeljek, 29. april, 2013"
    German: "Montag, 29. April, 2013"



    • Edited by Jernej Kavka Monday, April 29, 2013 1:21 PM
    • Proposed as answer by Jernej Kavka Monday, April 29, 2013 1:21 PM
    • Marked as answer by Aaron Xue Wednesday, May 8, 2013 10:08 AM
    Monday, April 29, 2013 1:12 PM
  • The DateTimeFormatter class is used to format date and times. This is the reason why the sample (and its documentation) were referred to. That class uses 'patterns' and 'structures' you'll need to get acquainted with in order to achieve your goal. F.i. in the  "OverrideSettings" UserControl [adapt to your busines cases]:
    new DateTimeFormatter(YearFormat.Abbreviated,
                        MonthFormat.Abbreviated,
                        DayFormat.Default,
                        DayOfWeekFormat.Full,
                        HourFormat.None,
                        MinuteFormat.None,
                        SecondFormat.None,
                        new[] { "de-DE" },
                        "DE",
                        CalendarIdentifiers.Gregorian,
                        ClockIdentifiers.TwelveHour),

    • Marked as answer by Aaron Xue Wednesday, May 8, 2013 10:08 AM
    Monday, April 29, 2013 1:13 PM