Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am working on Localization for .net web application for the first time. When I am selecting the English (en-US) it's working fine. But when I'm selecting/switching to French, Spanish and German not getting AM/PM.

It's printing like 11:56:32

Can you please help on this to get AM/PM by using above Languages?

What I have tried:

protected void Page_Load(object sender, EventArgs e)
    {
lblErrorMessage.Text = DateTime.Now.ToString("hh:mm:ss tt")

}

protected override void InitializeCulture()
    {

        if (Session["Lang"] != null)
            language = Session["Lang"].ToString();

        if ((language != null) && (language != ""))
        {
            if (language == "English")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0].Trim());
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0].Trim());
            }
            else
            {
                switch (language)
                {
                    case "English":
                        language = "en-US";
                        break;
                    case "French":
                        language = "fr-FR";
                        break;
                    case "Spanish":
                        language = "es-ES";
                        break;
                    case "German":
                        language = "de-DE";
                        break;
                }

                Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                //Resources.Resource.Login;

            }
        }
        else
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0].Trim());
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0].Trim());
        }
    }
Posted
Updated 7-Jun-21 20:40pm
v2
Comments
Richard MacCutchan 8-Jun-21 3:28am    
European locales generally use the 24 hour clock for times, based on the system regional settings. You would need to create a custom locale based on the system and modify the time settings. But this is not a good idea as it means the user would see times in an unfamiliar format.
Bojjaiah 8-Jun-21 3:42am    
Thank you, for your reply. Can we have an example? @Richard
Richard MacCutchan 8-Jun-21 3:54am    
The MSDN documentation has examples.
[no name] 8-Jun-21 13:39pm    
https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.datetimeformat?view=net-5.0
BillWoodruff 8-Jun-21 20:43pm    
Here in Thailand, we have don chao, 6am~12:59pm, don bai 1pm~4pm, don yen 4pm~7pm, don thum 7pm~12am, don dhee 12am~5am: you can see why the 24 hour format (nellikar) is used in the digital domain, business, etc. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900