WinRT apps and Regional settings. The correct way to format dates and numbers based on the user's regional settings?

前端 未结 4 755
-上瘾入骨i
-上瘾入骨i 2020-12-02 10:40

I\'m having some problems in Windows 8 Metro apps (XAML & C#) regarding the user\'s regional settings. It seems that the apps won\'t respect user\'s regional set

4条回答
  •  醉梦人生
    2020-12-02 11:21

    This post still seems to be relevant even though it was asked two years ago. I just came across it as I was looking for an answer to about the same thing. I also wanted to display dates in the regional format in my WP8.1 WinRT app. The information posted here helps, but it was a bit hard to piece it together.

    This is what I came up with and it seems to work for me as the answer I needed:

    using Windows.Globalization;
    using Windows.Globalization.DateTimeFormatting;
    
    private string FormatDate(int year, int month, int day)
    {
        GeographicRegion userRegion = new GeographicRegion();
        string regionCode = userRegion.CodeTwoLetter;
        var formatter = new DateTimeFormatter("year month day", new[] { regionCode });
        DateTime dateToFormat = new DateTime(year, month, day);
        var formattedDate = formatter.Format(dateToFormat);
        return formattedDate;
    }
    

提交回复
热议问题