JSON string in an AJAX call - how to handle dates from different cultures in the code behind

37 views Asked by At

I am working on a website that handles inputs from logged-in users in the USA and Chile. Since the users are logged-in, the code knows the culture of each user. In the date-picker in the form on the browser, the dates are displayed in the culture-specific formats, i.e., mm/dd/yyyy for the users in the USA, and dd-mm-yyyy for the users in Chile.

The website was initially developed for the users in the USA only. So, when a user enters the inputs in a form, and then saves it, on the client side, that "JSON.stringifies" all input values from the form and passes that to a web method in the code-behind, which in turn saves all values into the database.

The web method accepts the date as a "Date" type. The problem is, if 13-02-2024 is passed as the date, the server complains that the date is invalid, but if 02/13/2024 is passed, everything runs smoothly. Why is that? (See the settings in the web.config file and the master page below.)

I have resolved this issue by handling the date in the client-side, so that the date passed to the code-behind is always in the format mm/dd/yyyy. But I wonder if there is a solution preferably at the global level, so that I don't have to touch every single instance where this can happen.

So, in gist, I have the following two questions:

1. Why is the server expecting the date in the mm/dd/yyyy format?

(in the web.config, this line is there:

 <globalization culture="auto" uiCulture="auto" requestEncoding="utf-8" responseEncoding="utf-8"/>`

In addition to that, each page is set to the user's culture by the following lines of pseudo-code in the Page_Init event of the master page:

Dim u = GetLoggedInUser()
contentPlaceHolderBody.Page.UICulture = u.Culture
contentPlaceHolderBody.Page.Culture = u.Culture

)

2. Is there an elegant way to handle the date from these two cultures on the server side without updating each and every javascript file that the different webpages are using? In other words, how do you experts handle situations like this?

Thanks in advance!

0

There are 0 answers