On which layer should the date be formatted?

Which layer of the system should format the date? Should the date when it is selected from the database (data level) be formatted or must be formatted at the presentation level?
I think formatting is a visual preference, so I would put it as close to the presentation level as possible.

Where do you save the format string for your preferred date?

+4
source share
6 answers

The date (as well as money and other number formats) should be formatted at the presentation level. The format string will usually be part of the locale information. I would suggest reading internationalization (also known as i8n). Given that your nickname is C # 'ish, here you can find one place:

Microsoft.Net Internationalization

+7
source

You must format the date at the presentation level. I usually put the date property as a string type in my ViewModel and would have formatting when setting to ViewModel. Store it as a regular date object in your entity.

+1
source

The date should be formed as close as possible to the presentation, as you declare.

If the OS is already generating information (localization), use it and do not keep a copy!

+1
source

Absolutely yes, formatting takes place at the presentation level. Up to this point, you keep the data in all internal formats most suitable - for dates, which can be either DATETIME or the equivalent, or as a binary representation; which is best for sorting, storing, etc.

You don’t turn it into anything that actually looks like a date until it is displayed (or otherwise displayed in a human-readable form), at which point you convert it and apply any regional or custom formatting attributes.

These attributes are essentially your “format string” and you store them in a database (if centralized) or, possibly, a local configuration file on the user's PC. Personally, I tend to maintain standard or standard default rules in the database, and then add overrides for a specific user or region at the display point.

+1
source

All formatting must be done at the IMHO presentation level.

0
source

Another vote for presentation-level formatting. I would go further and see if the dates should be stored in UTC in the database, and the conversion is faster in the stream.

0
source

Source: https://habr.com/ru/post/1300793/


All Articles