iman kari
iman kari

Reputation: 93

C# - How to change culture without changing DateTime format

I have been used Resx file in ASP,net Web Form for implemention of multi language in an site. for changing the language I should change below mesures:

Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentUICulture

it seems that in new operating systems such as Microsoft Windows 10 , changing Culture may change the DateTime in a same way. it cause a lot of problem for us for example : The saving format for DateTime in database is Anno Domini but it change by changing Culture and save in database by other formats. How should I change the Culture without changing the Date format?

Thanks

Upvotes: 4

Views: 2702

Answers (2)

iSafa
iSafa

Reputation: 175

I added the following code to my OnLoad of master page to prevent using other cultures calendars

System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat = New CultureInfo("en-GB").DateTimeFormat

Upvotes: 2

Mojtaba Tajik
Mojtaba Tajik

Reputation: 1733

You should implement custom culture using CultureInfo class and derive datetime patterns from default or own culture :

public static class PersianDateExtensionMethods
{
    private static CultureInfo _culture;

    public static CultureInfo GetPersianCulture()
    {
        const string cultureName = "fa-IR";

        if (_culture == null)
        {
            _culture = new CultureInfo(cultureName);

            DateTimeFormatInfo formatInfo = _culture.DateTimeFormat;

            formatInfo.AbbreviatedDayNames = new[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
            formatInfo.DayNames = new[] { "یکشنبه", "دوشنبه", "سه شنبه", "چهار شنبه", "پنجشنبه", "جمعه", "شنبه" };
            var monthNames = new[]
            {
                "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن",
                "اسفند",
                ""
            };
            formatInfo.AbbreviatedMonthNames = formatInfo.MonthNames = formatInfo.MonthGenitiveNames = formatInfo.AbbreviatedMonthGenitiveNames = monthNames;

            _culture.DateTimeFormat = new CultureInfo("en-US").DateTimeFormat;

            Calendar persianCalendar = new PersianCalendar();

            FieldInfo fieldInfo = _culture.GetType().GetField("calendar", BindingFlags.NonPublic | BindingFlags.Instance);
            if (fieldInfo != null)
                fieldInfo.SetValue(_culture, persianCalendar);

            FieldInfo info = formatInfo.GetType().GetField("calendar", BindingFlags.NonPublic | BindingFlags.Instance);
            if (info != null)
                info.SetValue(formatInfo, persianCalendar);

            _culture.NumberFormat.NumberDecimalSeparator = "/";
            _culture.NumberFormat.DigitSubstitution = DigitShapes.NativeNational;
            _culture.NumberFormat.NumberNegativePattern = 0;
        }
        return _culture;
    }
}

As you can see in code DateTimeFormat of our custom culture set to date time format of "en-US" culture, at the end _culture variable contain a new culture which is for Persian language but the date time format inherited from US culture.

You need to configure services to use your culture like this :

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture =
            PersianDateExtensionMethods.GetPersianCulture();

I don't know where is your problem to change the code for your requirements but i hope this code gives you an idea to solve the problem.

Upvotes: 0

Related Questions