We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37286
    • 160 Posts
    I'm trying to build an addon and I am having a problem with the datefield. I want to be able to update the release date from the grid, and it appears to work, but it displays "0NaN-NaN-NaN" instead of the date. If I change the xtype to textfield, the date displays fine. I have also tried to format the date. Below is my base java code.

            ,fields: ['id','name','description','release_date','menu']
            ,columns: [{
                header: _('id')
                ,dataIndex: 'id'
                ,sortable: true
                ,width: 30
            },{
                header: _('name')
                ,dataIndex: 'name'
                ,sortable: true
                ,width: 100
                ,editor: { xtype: 'textfield' }
            },{
                header: _('description')
                ,dataIndex: 'description'
                ,sortable: false
                ,width: 350
                ,editor: { xtype: 'textfield' }
            },{
                header: _('release_date')
                ,dataIndex: 'release_date'
                ,sortable: false
                ,width: 80
                ,editor: { xtype: 'datefield' } 
                ,allowBlank: true
            }]
    


    If anyone has any ideas or if I need to post more info, please let me know.
    Thanks
      • 37286
      • 160 Posts
      OK, found a solution to this for anyone else having the same issue.

      I changed the schema of the field for my date to an integer:
      <field key="release_date" dbtype="int" precision="10" phptype="integer" null="true"/>
      


      Get list php page
      /* iterate */
      $list = array();
      foreach ($fields as $field) {
          $fieldArray = $field->toArray();
          /* format date */
          $fieldArray['release_date'] = !empty($fieldArray['release_date']) ? strftime('%m/%d/%Y',$fieldArray['release_date']) : '';
          $list[]= $fieldArray;
      }
      


      Create/Update php page called before the $modx->newObject / $fields->fromArray($scriptProperties);
      $scriptProperties['release_date'] = strtotime($scriptProperties['release_date']);
      


      This seams to work well, does anyone has a better way?
        • 33968
        • 863 Posts
        I'm doing the same - although stored with a 'timestamp' phptype. That actually requires additional date formatting as MODX seems to automatically format timestamps into the ISO date format.

        If I want something different from that I have to format back to a timestamp then back to a string in my desired format: Timestamp > ISO > Timestamp > Date string.

        I end up doing this in my iteration:
            $discArr['date_start'] = strftime('%d %b %Y',strtotime($discArr['date_start']));