Sunday, April 3, 2011

MVC & Date Formatting

Hi, Where in an MVC based architecture should the date formatting (long/short/time/no-time/UK/US) live? I want to be able to specify long/short/time/no-time on a per-field basis - i.e. DateOfBirth doesn't care about time, but CreationDate might.

Using ASP.NET MVC, there doesn't seem an easy way to do it in the View, which makes me wonder if it should sit in the ViewModel as a FormattedDate field?

Any suggestions?

Many thanks, Chris

From stackoverflow
  • First of all there is a difference between (1:long/short), (2:time/no-time) and (3:UK/US). 1. is definitely a view choice to make, 2. Effectively describes to data types ("DateTime", "Date" (which doesn't exist and you'd need to create)) and 3. is user profile thing, which again belongs in the view.

    So far then you only have two types DateTime (which exists) and Date (which doesn't). These are the types that you can expect the model to expose.

  • I read this as two parts:

    • Formatting (long, short)
    • Data type (date and time, just date, just time)

    The first part is a view concern, mostly, and should live in the presentation. I'd most likely just craft some extension methods for the DateTime/Date/Time that include the required translation or perhaps even create presentation versions of the types.

    The second part are model concerns, since a date of birth (usually) doesn't include time, regardless of the way you present it, and a starting time for a recurring lecture shouldn't have a date (since it's recurring, it would actually have a start/end). Sadly .net doesn't include (as far as I can recall) separate objects for Date and Time, so I roll my own, usually.

0 comments:

Post a Comment