2019-11-18 05:58:33 +01:00
---
2014-10-19 10:32:34 +02:00
title: DateField
summary: How to format and use the DateField class.
2019-11-18 05:58:33 +01:00
---
2014-10-19 10:32:34 +02:00
# DateField
2017-02-14 06:19:09 +01:00
This `FormField` subclass lets you display an editable date, in a single text input field.
2017-04-03 02:00:59 +02:00
It implements the [HTML5 input date type ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date )
(with `type=date` ). In supported browsers, this will cause a localised date picker to appear for users.
HTML5 date fields present and save ISO 8601 date formats (`y-MM-dd`),
since the browser takes care of converting to/from a localised presentation.
Browsers without support receive an `<input type=text>` based polyfill.
2014-10-19 10:32:34 +02:00
The following example will add a simple DateField to your Page, allowing you to enter a date manually.
2018-06-25 00:39:53 +02:00
**app/code/Page.php**
2014-10-19 10:32:34 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Forms\DateField;
use SilverStripe\CMS\Model\SiteTree;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class Page extends SiteTree
{
private static $db = [
'MyDate' => 'Date',
];
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(
'Root.Main',
DateField::create('MyDate', 'Enter a date')
);
return $fields;
}
}
2017-08-03 02:51:32 +02:00
```
2014-10-19 10:32:34 +02:00
## Custom Date Format
2017-07-03 03:22:12 +02:00
A custom date format for a [DateField ](api:SilverStripe\Forms\DateField ) can be provided through `setDateFormat` .
2017-04-03 02:00:59 +02:00
This is only necessary if you want to opt-out of the built-in browser localisation via `type=date` .
2014-10-19 10:32:34 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
// will display a date in the following format: 31/06/2012
DateField::create('MyDate')
->setHTML5(false)
->setDateFormat('dd/MM/yyyy');
2017-08-03 02:51:32 +02:00
```
2014-10-19 10:32:34 +02:00
2019-11-18 05:58:33 +01:00
[info]
2017-03-30 23:37:21 +02:00
The formats are based on [ICU format ](http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details ).
2019-11-18 05:58:33 +01:00
[/info]
2014-10-19 10:32:34 +02:00
## Min and Max Dates
Sets the minimum and maximum allowed date values using the `min` and `max` configuration settings (in ISO format or
2017-03-30 23:37:21 +02:00
`strtotime()` ).
2014-10-19 10:32:34 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
DateField::create('MyDate')
->setMinDate('-7 days')
->setMaxDate('2012-12-31')
2017-08-03 02:51:32 +02:00
```
2014-10-19 10:32:34 +02:00
## Formatting Hints
It's often not immediate apparent which format a field accepts, and showing the technical format (e.g. `HH:mm:ss` ) is
of limited use to the average user. An alternative is to show the current date in the desired format alongside the
field description as an example.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$dateField = DateField::create('MyDate');
2014-10-19 10:32:34 +02:00
2017-10-27 04:38:27 +02:00
// Show long format as text below the field
$dateField->setDescription(_t(
'FormField.Example',
'e.g. {format}',
[ 'format' => $dateField->getDateFormat() ]
));
2014-10-19 10:32:34 +02:00
2017-10-27 04:38:27 +02:00
// Alternatively, set short format as a placeholder in the field
$dateField->setAttribute('placeholder', $dateField->getDateFormat());
2017-08-03 02:51:32 +02:00
```
2014-10-19 10:32:34 +02:00
2019-11-18 05:58:33 +01:00
[notice]
2017-07-03 03:22:12 +02:00
Fields scaffolded through [DataObject::scaffoldCMSFields() ](api:SilverStripe\ORM\DataObject::scaffoldCMSFields( )) automatically have a description attached to them.
2019-11-18 05:58:33 +01:00
[/notice]
2014-10-19 10:32:34 +02:00
## API Documentation
2017-07-03 03:22:12 +02:00
* [DateField ](api:SilverStripe\Forms\DateField )