Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Suppose that you want to show a user list, on a web application having to handle custom date formats (depending on current selected interface language).

Several locations for applying the date format seem possible. Which one would you use, and why?

  • At model, for example doing a DATE_FORMAT(user.birthdate, '%d/%m/%Y') for date fields (supposing we have a MySQL DB).

  • At controller level, for example (supposing we use PHP):

    $users = UserModel::getUsers();
    // loops through results before display to format dates.
    // in the $users structure, we have to know which fields are dates or not
    $users = MagicFunctionFormattingDates($users);
    
    $view = new View('someTemplate');
    $view->assign('users', $users);
    
  • At view level, for example (supposing we use a template language such as Smarty):

    {foreach key=keyitem item=row from=$users}
      <tr>
      {foreach key=keyfield item=field from=$row.fields}
         {* Here $field should be an Object or associative array
            holding its 'type' (is it a date or something else) *}
         <td>{MagicFunctionForFormatting field=$field}</td>
      {/foreach}
      </tr>
    {/endforeach}
    

A similar and related question goes for sumbitting dates (e.g. on a user registration form). Where should the date conversion take place:

  • In the controller processing and validating the form, before calling the model?

  • In the model?

share|improve this question
add comment

5 Answers 5

up vote 5 down vote accepted

I'd suggest two things:

  1. The model is where your database I/O happens. In that case you want everything going in and out of the database to be a a predefined form. This is your "neutral" form, the defined interface to everything else which must always be consistent.

  2. After getting 1 clear, you can do transforms somewhere else. Where else is up to your design.

HOWEVER... in thinking about this a bit more, suppose the date format changes. Suppose, further, that you allow date format to be presented according to user preferences.

In that case you would seem to have 2 choices:

  1. Model: the interface is in the users preferred format. OR

  2. Model: the interface is always the neutral format, in which case, VIEW should probably be where you transform to/from the way that the user gets to see it.

Going with option 2, this means that controller can also always work with the neutral format meaning that coding functions/operations in it might be easier and/or less prone to change or reworking.

However, this does all depend on what you are trying to achieve.


My final take on all this: keep to a neutral form as long as you possibly can and then convert to/from the user preferred form as late as you possibly can. (And this probably means View is where your transforms should take place).

share|improve this answer
2  
Be careful of your definition of Model. It doesn't just mean data model, it means domain model -- ie. everything that isn't View or Controller. thedeveloperday.com/skinny-controllers –  pdr Dec 22 '11 at 12:29
    
@pdr - agreed. I used the principle that changes should be confined to a single place whenever possible, and then try to find the most sensible way of getting that outcome. The total code is easier to modify and maintain if going that way. –  quickly_now Dec 23 '11 at 1:34
    
+1 Brings good clarity to subject. –  Dipan Mehta Dec 23 '11 at 3:31
add comment

It's really entirely up to you.

I would consider it a display detail. If you were to replace the front-end with a web service, you would want to "display" it in an entirely different way. And thus I'd put it in the view. Personally.

share|improve this answer
add comment

The general rule you want to convert from/to date types as close to the user as possible - really this is true of all values but especially dates.

Dates that are stored as a proper date type are unambiguous (or as close as we're going to get) as soon as you go to a string you've got all kinds of opportunities for things to go wrong. So, unless you have good reason to do otherwise (which one does from time to time), you want to convert in both directions in the UX or as close to is as possible.

If these were numbers you probably wouldn't even be asking and yet its essentially the same thing.

share|improve this answer
add comment

I like handling date presentation (view) and encoding (submit) at the viewer. I immediately convert it to my desired back-end date format, and stick with that format in the back-end.

One caveat - every "good" back-end system I've developed has been integrated with other systems, necessitating some amount of date conversion in the back-end, as well. I usually defer that problem to if/when these additional systems need it, and stick with the plan I noted above.

share|improve this answer
add comment

Several locations for applying the date format seem possible. Which one would you use, and why?

The view, because text formatting is view-related.

A similar and related question goes for submitting dates (e.g. on a user registration form). Where should the date conversion take place?

The model, via an appropriate setter method.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.