Home Website date format changes after uploading even though cultures are set globally
Reply: 1

Website date format changes after uploading even though cultures are set globally

Joshua Parsons
1#
Joshua Parsons Published in 2018-02-14 08:38:06Z

I have created a form that uses a bootstrap datepicker to allow the user to input the date, and the methods used work perfectly when testing on my localhost. A problem occurred when i uploaded the site to do a final test which switches the date format throughout my site from dd/MM/yyyy to dd-MM-yyyy even though i have converted the date using global.asax culture settings. This date format wouldn't be such an issue but my forms no longer submit and the error i get says "sequence contains no elements" I have checked all settings on both the server and in the site that could be causing this but cannot seem to fix the issue. Thanks in advance for any suggestions.

Convert the date to the specified format on the front end:

.GetValueOrDefault().ToString("dd/MM/yyyy")

Global.asax code:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    CultureInfo newCulture = new CultureInfo("en-ZA");
    newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    newCulture.DateTimeFormat.DateSeparator = "/";
    Thread.CurrentThread.CurrentCulture = newCulture;
    Thread.CurrentThread.CurrentUICulture = newCulture;
}

Controller Code:

_vehicle[c] = new Vehicle()
{
    inceptionDate = string.IsNullOrEmpty(frm["inception_" + c]) ? 
    (IsFleetClient ? DateTime.Now.Date.AddDays(1) : DateTime.Now.Date) : 
    Convert.ToDateTime(frm["inception_" + c]).ToLocalTime()
};
easuter
2#
easuter Reply to 2018-02-16 08:18:37Z

I'm by no means well-versed in ASP.NET, but setting the culture on the current thread on app startup doesn't seem right - IIS is multithreaded so that may not be the thread that handles your requests later.

Try setting the culture globally in the Web.Config file, as shown here:

https://stackoverflow.com/a/7216126/1138552

EDIT:

Additionally it seems that en-ZA's ShortDatePattern is yyyy/MM/dd and not dd/MM/yyyy. This can be tested in PowerShell:

(New-Object CultureInfo "en-ZA").DateTimeFormat.ShortDatePattern

outputs (on my machine):

yyyy/MM/dd

If you'd like to change this to dd/MM/yyyy try setting up the culture and ShortDatePattern in Application_PreRequestHandlerExecute.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352477 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO