Sebastian
Sebastian

Reputation: 4811

How to handle Datetime for different ui cultures

In a web mvc5 application which supports en_Us and nb-NO cultures , How can i handle a Datetime object. I have a ExpiryDate property in model . How can i define the ExpDate property? What DisplayFormat i have to apply ? How can i convert that back in UI as per culture ? How can i save the data to SQlServer db as a valid datetime etc

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:DD.MM.YYYY}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> ExpDate { get; set; }

Does this helps [ which is not happening to me ]. Still confused on how to use the model in Controller and inside View while fecthing and saving data

Is any one knows some well written articles covering these aspects or any one can post some examples s\to solve this?

Upvotes: 1

Views: 483

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

In general a good approach to handling these Globalization issues is to always save DateTimes as UTC on the database and also to work with UTC format at the business logic level.

In fact since you do not know until it is render time at UI which culture the user selected to see ( and not only the culture but also the time-zone ), better to stick and work with UTC values and also with a format independent approach, meaning that you should not convert the DateTime from/to string value ever anywhere else than to render in the UI or to get it from data entry form.

If you did not read anything else up to now, I would start with this article: Beginner's Tutorial on Globalization and Localization in ASP.NET MVC

or simply google by ASP.NET MVC Globalization.

good luck

Upvotes: 1

Related Questions