logo

MMBase tips

Default dates and times

tip #17 / Wed 22 Sep 2010 / André / 0 comments

Setting the creation or a last modification date and time of a node is quite easy with mmbase datatypes.

A creation date can be based on a pre-defined mmbase datatype.

<field name="created" readonly="true">
  <descriptions>
    <description xml:lang="en">Creation date of this item</description>
    <description xml:lang="nl">Creatiedatum van het item</description>
  </descriptions>
  <gui>
    <guiname xml:lang="en">Created</guiname>
    <guiname xml:lang="nl">Aangemaakt</guiname>
  </gui>
  <editor>
    <positions>
      <input>9</input>
      <list>9</list>
      <search>9</search>
    </positions>
  </editor>
  <datatype base="created" xmlns="http://www.mmbase.org/xmlns/datatypes">
    <default value="now" />
  </datatype>
</field>

When the builder is first installed a datetime field is created in your database and the datatype code in mmbase makes sure its correctly filled. Whenever a new node of this type is made the date and time are set upon the default value 'now'.

Last modification dates and times are just as easy.

<field name="lastmodified" readonly="true">
  <descriptions>
    <description xml:lang="en">Date and time the item was last modified</description>
    <description xml:lang="nl">Datum en tijd waarop het item het laatst werd gewijzigd</description>
  </descriptions>
  <gui>
    <guiname xml:lang="en">Lastmodified</guiname>
    <guiname xml:lang="nl">Laatst veranderd</guiname>
  </gui>
  <editor>
    <positions>
      <input>8</input>
      <search>8</search>
    </positions>
  </editor>
  <datatype base="lastmodified" xmlns="http://www.mmbase.org/xmlns/datatypes" />
</field>

Whenever a node is modified its modification date is altered. Both the 'created' and 'lastmodified' example are of course read-only fields which can not be modified by users.

A 'normal' date field

A more generic use of a date is for example when an item is or should be published. This one should not be set automatically but by a user.

<field name="date">
<descriptions>
<description xml:lang="nl">Tijd en publicatiedatum van dit item</description>
<description xml:lang="en">Date and publish time of this item</description>
</descriptions>
<gui>
<guiname xml:lang="nl">Datum</guiname>
<guiname xml:lang="en">Date</guiname>
</gui>
<editor>
<positions>
<input>8</input>
<list>8</list>
<search>8</search>
</positions>
</editor>
<datatype base="datetime" xmlns="http://www.mmbase.org/xmlns/datatypes">
<default value="today" />
<minInclusive enforce="never" value="today - 10 year" />
<maxInclusive enforce="never" value="today + 99 year" />
</datatype>
</field>

When a node it belongs to is first created it gets the date and time of creation, because normally a publication date would not differ very from its creation date. But in stead of just a string being printed the user is offered various pulldowns to change year, month, day and time. To limit the length of the pulldown with years, which can in theory be infinite, it ranges from 10 years before this year ('minInclusive') untill 99 years from this year ('maxInclusive').

More information about defining builders for nodetypes in mmbase, like for example last creator or last modifier, can be found in the mmdocs.

0 comments

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm