1

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()
};
dferenc
  • 7,918
  • 12
  • 41
  • 49

1 Answers1

0

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.

easuter
  • 1,167
  • 14
  • 20
  • I gave this a shot and now my dates display as 12/mm/02/mm/2018? Any ideas? – Joshua Parsons Feb 14 '18 at 09:53
  • `12/mm/02/mm/2018`?...that doesn't seem like something that the Framework culture would do; have you removed any previous `DateTimeFormat.ShortDatePattern`s you may have left lying around? – easuter Feb 14 '18 at 15:41
  • Alright, using your answer i managed to find a few things wrong with my application which are now fixed and the entire thing is working again. However the time format is now yyyy/mm/dd but i need it to be dd/mm/yyyy. i have set all dates to convert all strings ToShortDateString() and i have set the format of this pattern in global.asax to no effect. – Joshua Parsons Feb 15 '18 at 14:03
  • I suspect that if you're still setting this on the `CurrentThread` in `Application_Start` then it won't have the desired effect for *all* threads that IIS may spawn. Have you tried setting this in `Application_PreRequestHandlerExecute` instead? – easuter Feb 15 '18 at 14:20
  • Whats strange is that .net should use the date format of my local machine? both my PC and the server are set to dd/MM/yyyy. Just out of interest, i did try setting the format in Application_PreRequestHandlerExecute() but the date stayed the same – Joshua Parsons Feb 15 '18 at 15:29
  • Actually it seems that `en-ZA`'s `ShortDatePattern` is set to `yyyy/MM/dd`. You can test this yourself by debugging a `CultureInfo` object that's been setup for `en-ZA`. Or you could just test it in Powershell: `(New-Object CultureInfo "en-ZA").DateTimeFormat.ShortDatePattern` returns `yyyy/MM/dd` on my machine. – easuter Feb 15 '18 at 15:53
  • PS C:\Users\Joshua> (New-Object CultureInfo "en-ZA").DateTimeFormat.ShortDatePattern dd/MM/yyyy PS C:\Users\Joshua> – Joshua Parsons Feb 16 '18 at 07:22
  • 1
    I figured it out, instead of setting the culture in global.asax, i set it in CultureHelper.cs. It was set to en-ZA but i set it to gsw-FR which has fixed the issue. Thank you for all your help over the past few days. i used this awesome document to check the time and date formats of each culture http://www.basicdatepicker.com/samples/cultureinfo.aspx – Joshua Parsons Feb 16 '18 at 08:00
  • Glad you figured it out! Wish I could have provided better info in my original reply, I'm just not much of an ASP.NET buff. Cheers! – easuter Feb 16 '18 at 08:18