Wayneio
Wayneio

Reputation: 3576

Momentjs deprecation error and different browser functionality

I'm getting this error:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 1-June-2017, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.createFromInputFallback (http://localhost/client.min.js:46682:95)
    at configFromString (http://localhost/client.min.js:48540:12)
    at configFromInput (http://localhost/client.min.js:48909:10)
    at prepareConfig (http://localhost/client.min.js:48892:10)
    at createFromConfig (http://localhost/client.min.js:48859:41)
    at createLocalOrUTC (http://localhost/client.min.js:48946:13)
    at createLocal (http://localhost/client.min.js:48950:13)
    at Moment.isBefore (http://localhost/client.min.js:49517:49)
    at http://localhost/client.min.js:46163:87
    at Array.map (native)

Because the code is minimised, I can't see exactly what's causing it and there is a lot of code, but it's probably in either of these:

    this.state = {
    bookedDays: this.props.bookedDays,
    today: this.props.value != "" ? moment.unix(this.props.value.substr(0, this.props.value.indexOf('|'))/1000) : moment(),
    selectedFromDate: this.props.value != "" ? moment.unix(this.props.value.substr(0, this.props.value.indexOf('|'))/1000) : moment(),
    selectedToDate: this.props.value != "" ? moment.unix(this.props.value.substr(this.props.value.indexOf('|')+1)/1000) : moment(),
    whichDate: 0
};



  var liClasses = classNames({
    'whiteDate': this.state.whichDate == 0 && ((this.state.selectedFromDate.isBefore(date, 'day') && this.state.selectedToDate.isAfter(date, 'day')) || this.state.selectedFromDate.isSame(date, 'day') || this.state.selectedToDate.isSame(date, 'day'))
  });

Note that the code only works in Chrome, in populating the class correctly, Firefox, IE and Edge all don't seem to like it.

Upvotes: 0

Views: 484

Answers (1)

VincenzoC
VincenzoC

Reputation: 31502

The problem could be that date in the second part of your code is a string which format is not recognized by moment(String) function.

You should use moment(String, String). From the error message it seems that you are passing 1-June-2017 as input value, so you have to use:

moment(date, 'D-MMMM-YYYY')

Your code will be like the following:

var liClasses = classNames({
  'whiteDate': this.state.whichDate == 0 && 
    ((this.state.selectedFromDate.isBefore(moment(date, 'D-MMMM-YYYY'), 'day') && 
    this.state.selectedToDate.isAfter(moment(date, 'D-MMMM-YYYY'), 'day')) ||
    this.state.selectedFromDate.isSame(moment(date, 'D-MMMM-YYYY'), 'day') || 
    this.state.selectedToDate.isSame(moment(date, 'D-MMMM-YYYY'), 'day'))
});

Upvotes: 1

Related Questions