Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is the model :

e[DataType(DataType.Date)]

    public Nullable<System.DateTime> PublishDate { get; set; }
This is the view :

e   <dt>
            @Html.DisplayNameFor(model => model.PublishDate)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.PublishDate)
        </dd>
When I am on English version of the page , date renders in this way for example 14/08/2017 , and when i change language in French then date renders in this way 14.8.2017 . Why it is happining that , can anyone help me , for no matter language selected , the date format should always be the same .


What I have tried:

i explained the problem and my solution above.
Posted
Updated 17-Aug-17 5:20am
Comments
Namith Krishnan E 16-Aug-17 6:45am    
you are changing the language by culture right?
ddgjgj 16-Aug-17 6:47am    
Yes , and save that value in session.
@Html.ActionLink("EN", "ChangeCulturee", "Language", new { lang = "en", returnUrl = this.Request.RawUrl }, null)

Try this while chaging the culture. Set newCulture as your changed culture

CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
newCulture.DateTimeFormat.DateSeparator = "-";
Thread.CurrentThread.CurrentCulture = newCulture;






Replace with this code

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
//It's important to check whether session object is ready
if (HttpContext.Current.Session != null)
{
CultureInfo ci = (CultureInfo)this.Session["Culturee"];
//Checking first if there is no value in session 
//and set default language 
//this can happen for first user's request
if (ci == null)
{
//Sets default culture to english invariant
string langName = "en";

//Try to get values from Accept lang HTTP header
if (HttpContext.Current.Request.UserLanguages != null && 
HttpContext.Current.Request.UserLanguages.Length != 0)
{
//Gets accepted list 
langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
}
ci = new CultureInfo(langName);
this.Session["Culturee"] = ci;
}
//Finally setting culture for each request

ci.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
ci.DateTimeFormat.DateSeparator = "-";
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;

//Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);


  
}
}
 
Share this answer
 
v2
Comments
ddgjgj 16-Aug-17 6:56am    
This the global.asax


protected void Application_AcquireRequestState(object sender, EventArgs e)
{
//It's important to check whether session object is ready
if (HttpContext.Current.Session != null)
{
CultureInfo ci = (CultureInfo)this.Session["Culturee"];
//Checking first if there is no value in session
//and set default language
//this can happen for first user's request
if (ci == null)
{
//Sets default culture to english invariant
string langName = "en";

//Try to get values from Accept lang HTTP header
if (HttpContext.Current.Request.UserLanguages != null &&
HttpContext.Current.Request.UserLanguages.Length != 0)
{
//Gets accepted list
langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
}
ci = new CultureInfo(langName);
this.Session["Culturee"] = ci;
}
//Finally setting culture for each request
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
}



And this is the controller :

public ActionResult ChangeCulturee(string lang, string returnUrl)
{
Session["Culturee"] = new CultureInfo(lang);
return Redirect(returnUrl);
}


What i should change here , exactly , where ?!
Namith Krishnan E 16-Aug-17 7:10am    
solution updated
ddgjgj 16-Aug-17 7:13am    
GENIUS!
Namith Krishnan E 16-Aug-17 7:22am    
:)
If you want to control the format for a specific field, use the DisplayFormat attribute:
DisplayFormatAttribute Class | Microsoft Docs[^]
C#
[DisplayFormat("dd-MMM-yyyy")]
public DateTime? PublishDate { get; set; }
(NB: DateTime? is a shorthand way of writing Nullable<DateTime>.)

If you want to override the format for all dates, create a display template in Views\Shared\DisplayTemplates\Date.cshtml:
@model DateTime?
@if (Model.HasValue)
{
    @Model.Value.ToString("dd-MMM-yyyy")
}
and then add the DataType attribute to any properties which should only show the date:
C#
[DataType(DataType.Date)]
public DateTime? PublishDate { get; set; }


If you want to turn off automatic globalization for the entire application, you can change the web.config file. You probably have the culture set to "auto":
<configuration>
  <system.web>
    <globalization culture="auto" uiCulture="auto" />
  </system.web>
</configuration>
This sets the culture based on the user's preferred language.

Replace "auto" with the code of the culture you want to use. For example:
<configuration>
  <system.web>
    <globalization culture="en-GB" uiCulture="en-GB" />
  </system.web>
</configuration>

You can even have different settings for culture and UI culture - the culture drives number and date formatting, currency symbols, sorting, etc., whereas the UI culture is primarily used to determine which .resx files are used to look up localized resources.

Auto Selecting Cultures for Localization in ASP.NET - Rick Strahl's Web Log[^]
 
Share this answer
 

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