I have a ASP.NET MVC 4 with EF project. I'm changing the current thread culture when a user logs in.
Global.asax.cs
protected void Application_BeginRequest(object sender, EventArgs e)
{
CultureInfo objculture = new CultureInfo(Helios.MvcApplication.Lang); //Lang = en-US, ro-RO, fr-FR
//DateTimeFormatInfo objDTFI = objculture.DateTimeFormat;
//objDTFI.ShortDatePattern = "dd.MM.YY";
//objDTFI.DateSeparator = ".";
//objculture.DateTimeFormat = objDTFI;
objculture.NumberFormat.CurrencyDecimalSeparator = ".";
objculture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentUICulture = objculture;
Thread.CurrentThread.CurrentCulture = objculture;
}
DateTime.cshtml //EditorTemplate for my datetime
@model System.DateTime?
@Html.TextBox("", String.Format("{0:d}", Model.HasValue ? Model : DateTime.Today), new { @class = "datefield" })
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.20/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.8.20/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(".datefield").datepicker({
changeMonth: true, changeYear: true, firstDay: 1, changeYear: true,
dateFormat: 'dd.mm.yy'
});
});
If I use : String.Format("{0:yyyy-MM-dd}" or String.Format("{0:MM-dd-yyyy}" it works, but if I use String.Format("{0:dd.MM.yyyy}" it doesn't work.
Q: How can I change the datetime format to "dd.MM.yyyy" for every culture/language ?
Best Answer
Old question, but for others needing to change the string format irrespective of the current culture: