Search code examples
javascriptmomentjsx-editable

Moment.min.js this.format is not a funtion


I am trying to use a combodate in xeditable however, when I try to save moment.min.js says that this.format is not a function. This is coming from somewhere in moment.js so here is my code:

$('#start').editable({
  viewformat: 'DD-MM-YYYY',
  success: function(result, newValue){
    return $.ajax({
      url: '/Project/Edit',
      data: { id: '1', NewValue: newValue, type: 'StartDate' },
      success: function (result) {
        if (result.Success == 'Success') {
          notify('The Start Date was successfully updated.', 'success');
        } else {
          notify('The Start Date could not be updated at this time.', 'error');
        }
      }
    });
  }
}).on('hidden', function () { 
  $(this).parent().next().children().removeClass('disabled');
});

Here is the line that is called in moment.js

toString: function () { return this.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ")

This line in moment.js is called right after the editable success function is called.


Solution

  • I figured it out. I had to call the .format() method on newValue in the ajax call. That's how moment js was giving me that error. Here is the working code:

    $('#start').editable({
      viewformat: 'DD-MM-YYYY',
      success: function(result, newValue){
        return $.ajax({
          url: '/Project/Edit',
          data: { id: '1', NewValue: newValue.format('MM/DD/YYYY'), type: 'StartDate' },
          success: function (result) {
            if (result.Success == 'Success') {
              notify('The Start Date was successfully updated.', 'success');
            } else {
              notify('The Start Date could not be updated at this time.', 'error');
            }
          }
        });
      }
    }).on('hidden', function () { 
      $(this).parent().next().children().removeClass('disabled');
    });