开发者

What layer should a date be formatted?

What layer of a system should a date be formatted? Should the date be formatted when it is selected from the database (data layer) or 开发者_开发百科should it be formatted at the presentation layer?

I am thinking that the formatting is a visual preference so I would tend to put it as close to the presentation layer as possible.

Where do you store the format string for the preferred date?


The date (also like money and other numerical formats) should be formatted at the presentation layer. The format string would typically be part of the locale information. I'd suggest reading up on internationalization (also known as i8n). Given your moniker is C#'ish here is one place to look:

Microsoft .Net Internationalization


You should format your date at the presentation layer. I typically would put the date property as a string type on my ViewModel and have the formatting happen when setting it on the ViewModel. Store it as a regular date object in your entity.


The date should be formated as close to the presentation as feasable, just as you state.

If the OS already has formating information (localisation), use that and don't store a copy!


Absolutely yes, formatting happens in the Presentation Layer. Right up until that point you hold the data in whatever internal format makes most sense - for dates, that might be as a DATETIME or equivalent, or as a binary representation; whatever is best for sorting, storage, etc.

You don't turn it into anything that actually looks like a date until it needs to be displayed (or otherwise output in Human-readable form) at which point you convert it and apply any regional or customised formatting attributes.

Those attributes are essentially your 'formatting string', and you'd store those in the database (if centralised) or maybe the local configuration file on the users' PC. Personally, I tend to keep 'standard' or 'common' default rules in the database, and then add user-specific or region-specific overrides at the point of display.


Another vote for formatting at presentation layer. I'd go one further, and look at if dates should be stored in UTC in the database, and conversion happens higher in the flow.


All formatting should be done at the presentation layer IMHO

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜