Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate date depending on region

I have an MVC4 app which is compiled in the UK and hosted on a US server. In my model I have a date field which is required.

Attached to this field is a date picker, on selecting a date it will set a date to be UK format "DD/MM/YYYY" e.g. 23/12/2013. On submitting the form the application throws a validation error as it does not expect the the format.

In MVC4 how do I:

  1. Accept both date formats i.e. Override the date format? I have set the date format but cannot specify multiple date formats
  2. Server side I am saving the date to a database using Linq. This also throws an error as an incorrect format. I suspect I will need to convert to a UK date?
  3. When loading the details put the date in the correct format depending on the user's location
  4. Detect the location of the user server side?

Has anyone got any details on how to very validation based on globalisation of the user?

Thanks

like image 298
CR41G14 Avatar asked Apr 15 '13 14:04

CR41G14


2 Answers

Did you add the below segment in web.config ?

<globalization 
    enableClientBasedCulture="true" 
    uiCulture="auto" 
    culture="auto" />

and read your date in code as DateTime format. that should not give you a problem.

Another way is to in you server side code read tha date always as DateTimeUTC and when ever you display the date on the screen, use .ToLocalTime()

like image 181
HaBo Avatar answered Oct 11 '22 12:10

HaBo


In the web config, in the globalization section, set the culture to "Auto". this will use user's current culture (including number and Date time format)

<globalization culture="Auto" />

The .net framework will parse and render data according to the user culture and both MVC and Entity will work properly.

like image 40
Stefano Altieri Avatar answered Oct 11 '22 11:10

Stefano Altieri