views:

84

answers:

6

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?

+1  A: 

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.

Keith Rousseau
+8  A: 

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

Peter M
I am with you on the globalization, but in some cases there are multiple 'acceptable' date formats for a culture...agree about the presentation layer...and looks like everybody does...
CSharpAtl
If there are multiple 'acceptable' date formats then I would think that they still need to be segmented out by locale.
Peter M
lexu
+1  A: 

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!

lexu
A: 

All formatting should be done at the presentation layer IMHO

SQLMenace
+1  A: 

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.

Jonners
A: 

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.

bryanjonker