Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » DateTime/datepicker format problems

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

Page: 1 2
Go to End
Author Topic: 2247 Views
  • kinglozzer
    Avatar
    Community Member
    138 Posts

    DateTime/datepicker format problems Link to this post

    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;
       }

    }

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: DateTime/datepicker format problems Link to this post

    Hi,

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

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

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: DateTime/datepicker format problems Link to this post

    Hi swaiba,

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

    Thanks for the quick reply,

    Loz

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: DateTime/datepicker format problems Link to this post

    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...

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: DateTime/datepicker format problems Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: DateTime/datepicker format problems Link to this post

    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');

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: DateTime/datepicker format problems Link to this post

    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: http://api.silverstripe.org/trunk/sapphire/forms/DateField_View_JQuery.html?

    Cheers,

    Loz

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: DateTime/datepicker format problems Link to this post

    FYI I am using ss 2.4.7, not trunk

    2247 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.