Moment: Momemtjs date converting error raise.

Created on 8 Oct 2016  路  18Comments  路  Source: moment/moment

Description of the Issue and Steps to Reproduce:
console.log('>>>>>>.', moment(value).format('MM-DD-YYYY'),moment(value).format('YYYY-MM-DD'));

I am not sure where this error generated, unable to track. I had added warning message.

Environment:
Ubuntu, chrome browser.

GETTING BELOW ISSUE

Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non 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: 27/04/2016, _f: undefined, _strict: undefined, _locale: [object Object]
Error

Most helpful comment

Doing moment(new Date("27/04/2016")).format....seems to work. But i think their are some issues in this approach, maybe related to locales etc.

All 18 comments

This warning message happens when you create a Moment from a string that's not in a supported ISO 8601 format (see http://momentjs.com/docs/#/parsing/string/ for a list of supported formats).

In this case, it looks like you tried to parse the string 27/04/2016, which is not an ISO format. You can parse this format without a warning if you tell Moment what the format is: moment("27/04/2016", "DD/MM/2016"). Alternatively, you can change the format of your input to match the ISO spec: moment("2016-04-27")

I'm closing this issue since it's not a bug in Moment, but if you still have questions, please mention me in a reply here and I'll try to help.

I am not successful, when i do a supported ISO 8601 format

Doing moment(new Date("27/04/2016")).format....seems to work. But i think their are some issues in this approach, maybe related to locales etc.

@ziaulrehmandevsinc Worked for me. Thanks.

@ziaulrehmandevsinc Also worked for me, Thanks

@ziaulrehmandevsinc It also worked for me. Thankyou so much

Worked for me. Thanks

At the moment, we would prefer not to adjust code to handle this issue. Within moment.js where it displays the warning:

function warn(msg) {
if (hooks.suppressDeprecationWarnings === false &&
(typeof console !== 'undefined') && console.warn) {
console.warn('Deprecation warning: ' + msg);
}

How does one set "hooks.suppressDeprecationWarnings"? I reviewed the hooks function but it reports hooks is not a function. Using aurelia.

Thanks

@pdesimone

// Suppress the warnings
const moment = require('moment');
moment.suppressDeprecationWarnings = true;

// See what else you can mess with
console.log(Object.keys(require('moment')));

Yeah I'm getting this issue using moment('2018-01-20') which, as far as I can tell is a valid ISO 8601 format.

At the time, I couldn't get the supressDepreciationWarnings or other solutions to work.

I have done this in all my uses of moment:

moment(value, format), ie: Value: 2018-01-20, Format: YYYY-MM-DD.

Then the warning doesn't happen. Apparently, if you let moment "try" and figure out the type of date, you get the warning...

@ziaulrehman40 worked for me, thanks so much

If you think you're handling things correctly and still seeing the error double check your input types <input type="date" /> will throw error but <input type="text" /> works.

@pdesimone

// Suppress the warnings
const moment = require('moment');
moment.suppressDeprecationWarnings = true;

// See what else you can mess with
console.log(Object.keys(require('moment')));

Thanks : Its working fine, but i need to know whether it will cause any issue while adding suppressdeprecationwarnings

Thanks, @ziaulrehman40 - :kissing_heart:

Instead of suppressing the warnings, Convert the date object to ISO string before formatting it using moment

 const moment = require('moment');

const dateFormatString = "YYYY-MM-DD";

function formatDate(dateObject) {
    var formatted = moment(dateObject.**toISOString()**).format(dateFormatString);
    return formatted;
}
var d = new Date();
var fd = formatDate(d);

console.log(fd);

Test it on Repl.it

I'm also having a trouble with this.

console.log(moment(this.entities[i].created_at).isValid());
this.entities[i].created_at = moment(this.entities[i].created_at).format("DD-MM-YYYY HH:mm");

The console log returns true, but I get the error message:

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: 21-05-2020 19:43, _f: undefined, _strict: undefined, _locale: [object Object]

A console.log on the date I receive returns 2020-05-21 19:43:58.099745+00.
Any suggestions?

@kiocosta Sorry you are having trouble. You should probably parse with a format. https://momentjs.com/docs/#/parsing/string-format/ It might be a good question for Stack Overflow too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danieljsinclair picture danieljsinclair  路  3Comments

ghost picture ghost  路  3Comments

BCup picture BCup  路  3Comments

ninigix picture ninigix  路  3Comments

slavafomin picture slavafomin  路  3Comments