Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

DateTime/datepicker format problems


Go to End
Reply


15 Posts   2836 Views

Avatar
kinglozzer

Community Member, 171 Posts

16 March 2012 at 11:24pm

Hi all,

This is driving me mad! I've searched the forums, the api, the internet for any clues that might help me with this problem and I haven't found anything that works. I'm trying to extend the Page class and add two fields - a start date and end date = for an events page on my site.

The fields appear fine, but whenever I choose a date with the datepicker, it fills the new field out in the wrong format - it uses MM/dd/YYYY when it should be returning dd/MM/YYYY.

Can anyone tell me what I'm doing wrong here?

I'm using the 3.0.0 beta, which I appreciate isn't a finished product, as I'm hoping to learn SilverStripe before v3 is officially released.

Thanks,

Loz

class EventPage extends Page {

   static $db = array (
      'EventStartDate' => 'Date',
      'EventFinishDate' => 'Date'
   );
   static $has_one = array (
   );
   
   function getCMSFields()
   {
      $fields = parent::getCMSFields();
      
      $fields->addFieldToTab('Root.Main', $startDateField = new DateField('EventStartDate','Event Start Date'), 'Content');
   $startDateField->setConfig('showcalendar', true);
   $startDateField->setConfig('dateformat', 'dd/MM/YYYY');
   
   $fields->addFieldToTab('Root.Main', $endDateField = new DateField('EventFinishDate','Event Finish Date'), 'Content');
   $endDateField->setConfig('showcalendar', true);
   $endDateField->setConfig('dateformat', 'dd/MM/YYYY');
   
      return $fields;
   }

}

Avatar
swaiba

Forum Moderator, 1805 Posts

16 March 2012 at 11:26pm

Hi,

if it helps this is how I do it...

$df->setConfig('showcalendar', true);
$df->setLocale('en_GB');
$df->setConfig('dateformat', 'dd/MM/YYYY');

Avatar
kinglozzer

Community Member, 171 Posts

16 March 2012 at 11:36pm

Hi swaiba,

I tried using setLocale() like you suggested but it made no difference.

Thanks for the quick reply,

Loz

Avatar
swaiba

Forum Moderator, 1805 Posts

16 March 2012 at 11:43pm

is you site setup with the right locale also? (you would have had defaulted to en_US on the ss install)

I *know* that code works on my systems...

Avatar
kinglozzer

Community Member, 171 Posts

16 March 2012 at 11:47pm

I'm sure I set the site up with UK locale, the following line is in mysite/_config.php:

i18n::set_locale('en_GB');

Unless I missed something and you can set it somewhere else?

Thanks

Avatar
swaiba

Forum Moderator, 1805 Posts

17 March 2012 at 12:10am

actually i think i see it...

$fields->addFieldToTab('Root.Main', $startDateField = new DateField('EventStartDate','Event Start Date'), 'Content');
$startDateField->setConfig('showcalendar', true);
$startDateField->setConfig('dateformat', 'dd/MM/YYYY');

try this instead...

$startDateField = new DateField('EventStartDate','Event Start Date')
$startDateField->setConfig('showcalendar', true);
$startDateField->setConfig('dateformat', 'dd/MM/YYYY');
$fields->addFieldToTab('Root.Main', $startDateField, 'Content');

Avatar
kinglozzer

Community Member, 171 Posts

17 March 2012 at 12:22am

No joy :(

Just to confirm; the field will accept the date if I input it manually in the correct format. It's just that the datepicker fills the form with the wrong format when choosing a date.

I wondered if it might have something to do with the DateField_View_JQuery class as detailed here: [url]http://api.silverstripe.org/trunk/sapphire/forms/DateField_View_JQuery.html[/url]?

Cheers,

Loz

Avatar
swaiba

Forum Moderator, 1805 Posts

17 March 2012 at 12:25am

FYI I am using ss 2.4.7, not trunk

Go to Top