New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be careful when falling back to Date constructor #1407

Closed
ichernev opened this Issue Jan 9, 2014 · 136 comments

Comments

Projects
None yet
@ichernev
Contributor

ichernev commented Jan 9, 2014

If you clicked on the warning link and landed here:

moment construction using a non-iso string is deprecated. What this means is you can safely do

> moment("2014-04-25T01:32:21.196Z");  // iso string, utc timezone
> moment("2014-04-25T01:32:21.196+0600");  // iso string with timezone
> moment("2014 04 25", "YYYY MM DD"); // string with format

but you can't do reliably (therefore its being deprecated)

> moment("2014/04/25");
> moment("Thu Apr 24 2014 12:32:21 GMT-0700 (PDT)");
> moment("some random string that looks like date");

In the upcoming releases a few more well defined formats might be added, but the general idea of using any random string to construct a date is no more. If you really want that behavior just do moment(new Date("random string that contains date")), but moment won't be responsible for such convertions. Read below for the rationale about the change

As described in #1403 (comment), but also recent debugging c4d9f6e and 94d6c90 caused by https://travis-ci.org/moment/moment/builds/16672288 brings me to the conclusion that

using Date to create a moment from string, passed from the user is extremely unpredictable, and it turns out to work somewhat and then bite you in an edge case when you're not careful

So I think we need an option to disable it, for tests at the very least. I've debugged that numerous times thank to our old friend IE6 and friends.

You'd pass a string only, hoping that it would hit one of the predefined formats, but instead it hits Date and it sort-of works, sometimes. I think it should be clear to users when they're hitting this case, and that it might not work well on all browsers.

The only use-case for using Date to parse input is when a) it is entered by a web user (moment's user's user :)) and b) the web user can see how we're interpreting the date entered. Every other case is just silently waiting to break at some point.

@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron Jan 9, 2014

Member

+1. I totally agree. We should start issuing a deprecation warning right away. Some additional support:

  • That a string might go into the Date constructor or the ISO parser is ugly and confusing
  • The most reliable way of using the Date string constructor was always new Date('2014-01-01'), which is now handled by the ISO parser anyway (i.e. moment('2014-01-01') doesn't use the Date constructor anyway).
  • I'm pretty sure the constructor works differently in different locales for the same browser. E.g. new Date("05/06/2014") is May 6 in American browsers but June 5 in English browsers. So even if you test all your browsers, did you test all your locales? It's terrible for us to even pretend this stuff works.
  • If you really want to use the date constructor, you can always just use the date constructor and pass the date into Moment.
  • We could make moment(string) extensible, and then someone could create a plugin that either uses a list of well-known formats or just the Date constructor to parse them.
  • If we remove the Date constructor fall-back, we can actually make the ISO parser less strict, since it won't be preempting a better parse. So that's a win too.
Member

icambron commented Jan 9, 2014

+1. I totally agree. We should start issuing a deprecation warning right away. Some additional support:

  • That a string might go into the Date constructor or the ISO parser is ugly and confusing
  • The most reliable way of using the Date string constructor was always new Date('2014-01-01'), which is now handled by the ISO parser anyway (i.e. moment('2014-01-01') doesn't use the Date constructor anyway).
  • I'm pretty sure the constructor works differently in different locales for the same browser. E.g. new Date("05/06/2014") is May 6 in American browsers but June 5 in English browsers. So even if you test all your browsers, did you test all your locales? It's terrible for us to even pretend this stuff works.
  • If you really want to use the date constructor, you can always just use the date constructor and pass the date into Moment.
  • We could make moment(string) extensible, and then someone could create a plugin that either uses a list of well-known formats or just the Date constructor to parse them.
  • If we remove the Date constructor fall-back, we can actually make the ISO parser less strict, since it won't be preempting a better parse. So that's a win too.
@ichernev

This comment has been minimized.

Show comment
Hide comment
@ichernev

ichernev Jan 14, 2014

Contributor

The idea I liked most is for extensible constructor -- even if we disable Date parsing in moment, we should make it easy for people to migrate.

So lets do this:

moment.createFromInputFallback = function(config) { config._d = new Date(config._i); };

And call this method in the else clause of makeDateFromInput. In 3.0 we'll just remove this function, or place it under another name, so that one can just assign to the preset.

For tests right now, we can put a different function that throws an exception if called. I hope that would work just fine.

Inside we can print using console.warn (would go to stderr in node), only the first time it is called (like global deprecation) -- this is to let users know what awaits them.

Contributor

ichernev commented Jan 14, 2014

The idea I liked most is for extensible constructor -- even if we disable Date parsing in moment, we should make it easy for people to migrate.

So lets do this:

moment.createFromInputFallback = function(config) { config._d = new Date(config._i); };

And call this method in the else clause of makeDateFromInput. In 3.0 we'll just remove this function, or place it under another name, so that one can just assign to the preset.

For tests right now, we can put a different function that throws an exception if called. I hope that would work just fine.

Inside we can print using console.warn (would go to stderr in node), only the first time it is called (like global deprecation) -- this is to let users know what awaits them.

@st3fan

This comment has been minimized.

Show comment
Hide comment
@st3fan

st3fan Apr 23, 2014

I don't understand this deprecation warning. I am using moment.js as documented, in an Angular.JS filter:

app.filter('scanDate', function() {
    return function(input) {
        var m = moment(input);
        return m.format("dddd, MMMM Do YYYY");
    };
});

Where input is an ISO formatted date string.

What do I need to do different to deal with this future deprecation?

If this deprecation warning is for the developers of this library and not for the users of it then I am going to suggest to remove it.

st3fan commented Apr 23, 2014

I don't understand this deprecation warning. I am using moment.js as documented, in an Angular.JS filter:

app.filter('scanDate', function() {
    return function(input) {
        var m = moment(input);
        return m.format("dddd, MMMM Do YYYY");
    };
});

Where input is an ISO formatted date string.

What do I need to do different to deal with this future deprecation?

If this deprecation warning is for the developers of this library and not for the users of it then I am going to suggest to remove it.

@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron Apr 23, 2014

Member

@st3fan Are you sure it's an ISO-formatted string? Moment is claiming that it isn't; it's saying that it can't parse it, and it is instead resorting to passing the string to new Date(s), which happens to work. That's the feature we're removing and are warning you about. In the future, it will fail. Your options are:

  1. Ensure that the input really is ISO-8601 compliant (and file a bug if you think it is but are getting the warning anyway)
  2. Parse the input explicitly using moment(input, string)

The warning is very much for you :)

Member

icambron commented Apr 23, 2014

@st3fan Are you sure it's an ISO-formatted string? Moment is claiming that it isn't; it's saying that it can't parse it, and it is instead resorting to passing the string to new Date(s), which happens to work. That's the feature we're removing and are warning you about. In the future, it will fail. Your options are:

  1. Ensure that the input really is ISO-8601 compliant (and file a bug if you think it is but are getting the warning anyway)
  2. Parse the input explicitly using moment(input, string)

The warning is very much for you :)

@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron Apr 23, 2014

Member

This is implemented, so I'm closing it.

Member

icambron commented Apr 23, 2014

This is implemented, so I'm closing it.

@icambron icambron closed this Apr 23, 2014

@Meekohi

This comment has been minimized.

Show comment
Hide comment
@Meekohi

Meekohi Apr 25, 2014

If we deprecate everything in Javascript that doesn't work on a side-case there will be no features left. ;) Looking forward to at least a few other "sane" date formats being added back in. Explicitly creating a Date or including the format string removes a lot of Moment's elegance.

Meekohi commented Apr 25, 2014

If we deprecate everything in Javascript that doesn't work on a side-case there will be no features left. ;) Looking forward to at least a few other "sane" date formats being added back in. Explicitly creating a Date or including the format string removes a lot of Moment's elegance.

@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron Apr 28, 2014

Member

@Meekohi We'll always allow ISO strings like '2013-05-10', which IMO everyone should just use. As for other strings, '05/10/2013' works differently in different locales, and most of the rest don't work across browsers. These aren't really side-cases; we receive an awful lot of support tickets here about surprises from things like moment("1.10.2014") where the user is expecting us to parse it consistently.

We are allowing this to be pluggable, so you can override the fallback parser (i.e. the thing currently giving a deprecation warning and then using the native parser) to do anything you'd like, and I suspect there will be some plugins built that do just that.

Member

icambron commented Apr 28, 2014

@Meekohi We'll always allow ISO strings like '2013-05-10', which IMO everyone should just use. As for other strings, '05/10/2013' works differently in different locales, and most of the rest don't work across browsers. These aren't really side-cases; we receive an awful lot of support tickets here about surprises from things like moment("1.10.2014") where the user is expecting us to parse it consistently.

We are allowing this to be pluggable, so you can override the fallback parser (i.e. the thing currently giving a deprecation warning and then using the native parser) to do anything you'd like, and I suspect there will be some plugins built that do just that.

@Meekohi

This comment has been minimized.

Show comment
Hide comment
@Meekohi

Meekohi Apr 28, 2014

@icambron Is the correct way to plugin your own parser to overwrite createFromInputFallback? i.e.

moment.createFromInputFallback = function(config) {
  // unreliable string magic, or
  config._d = new Date(config._i);
};

Meekohi commented Apr 28, 2014

@icambron Is the correct way to plugin your own parser to overwrite createFromInputFallback? i.e.

moment.createFromInputFallback = function(config) {
  // unreliable string magic, or
  config._d = new Date(config._i);
};
@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron Apr 28, 2014

Member

@Meekohi Correct.

Member

icambron commented Apr 28, 2014

@Meekohi Correct.

@hengkiardo

This comment has been minimized.

Show comment
Hide comment
@hengkiardo

hengkiardo Apr 29, 2014

hi guys,
i'm use moment on my node apps.
i using it something like this one

moment(Date('2014-04-21T05:29:59Z')).format("DD-MM-YYYY")

now when i check log on my server, i get error message on my log files

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

hengkiardo commented Apr 29, 2014

hi guys,
i'm use moment on my node apps.
i using it something like this one

moment(Date('2014-04-21T05:29:59Z')).format("DD-MM-YYYY")

now when i check log on my server, i get error message on my log files

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron Apr 29, 2014

Member

@aredo I think you're expecting Date(string) to return a Date? It appears to return a string. I think you want moment(new Date(string)) or moment(Date.parse(string)), or even just moment('2014-04-21T05:29:59Z')

Member

icambron commented Apr 29, 2014

@aredo I think you're expecting Date(string) to return a Date? It appears to return a string. I think you want moment(new Date(string)) or moment(Date.parse(string)), or even just moment('2014-04-21T05:29:59Z')

@thomasbird1984

This comment has been minimized.

Show comment
Hide comment
@thomasbird1984

thomasbird1984 May 1, 2014

I'm using an iso 8601 format like this: 2014-04-29 17:47:12, is there a list of the supported formats that will not be deprecated?

thomasbird1984 commented May 1, 2014

I'm using an iso 8601 format like this: 2014-04-29 17:47:12, is there a list of the supported formats that will not be deprecated?

@icambron

This comment has been minimized.

Show comment
Hide comment
@icambron

icambron May 1, 2014

Member

(Editing because I misunderstood the question and this is kind of an important doc now). @godoploid Only ISO 8601 are asp.net-JSON-style dates are not deprecated. On your specific format, that isn't ISO-compliant. Use a "T" instead of a space, as in "2014-04-29T17:47:12" (see here).

Member

icambron commented May 1, 2014

(Editing because I misunderstood the question and this is kind of an important doc now). @godoploid Only ISO 8601 are asp.net-JSON-style dates are not deprecated. On your specific format, that isn't ISO-compliant. Use a "T" instead of a space, as in "2014-04-29T17:47:12" (see here).

@thomasbird1984

This comment has been minimized.

Show comment
Hide comment
@thomasbird1984

thomasbird1984 May 1, 2014

@icambron Got it thanks, I'll make sure to do so!

thomasbird1984 commented May 1, 2014

@icambron Got it thanks, I'll make sure to do so!

@imsobear

This comment has been minimized.

Show comment
Hide comment
@imsobear

imsobear May 13, 2014

@icambron
But how I format the current time?

When i try this var now = moment().format('YYYY-M-D'), it warning me: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

imsobear commented May 13, 2014

@icambron
But how I format the current time?

When i try this var now = moment().format('YYYY-M-D'), it warning me: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

@ichernev

This comment has been minimized.

Show comment
Hide comment
@ichernev

ichernev May 13, 2014

Contributor

@imsobear I'm not sure the error message you see is from moment().format('YYYY-M-D'), I think its something before that.

Contributor

ichernev commented May 13, 2014

@imsobear I'm not sure the error message you see is from moment().format('YYYY-M-D'), I think its something before that.

@favio41

This comment has been minimized.

Show comment
Hide comment
@favio41

favio41 May 16, 2014

The documentation have a example that throws this Deprecation message.

Yeah I know, there is "warning" text, but if is for remove, maybe some "Deprecation" in the docs, could help the newbies.

moment("Dec 25, 1995")  -> "moment construction falls..."

http://momentjs.com/docs/#/parsing/string/

favio41 commented May 16, 2014

The documentation have a example that throws this Deprecation message.

Yeah I know, there is "warning" text, but if is for remove, maybe some "Deprecation" in the docs, could help the newbies.

moment("Dec 25, 1995")  -> "moment construction falls..."

http://momentjs.com/docs/#/parsing/string/

glasnt added a commit to WhereSoftwareGoesToDie/machiavelli that referenced this issue May 23, 2014

Correct formatting and validation for absolute time
moment.js deprecation warning and related issues: moment/moment#1407

@ideag ideag referenced this issue Jun 5, 2014

Closed

Invalid Date #34

@elliotf

This comment has been minimized.

Show comment
Hide comment
@elliotf

elliotf Jun 11, 2014

Contributor

Hello nice folks. I came here via the deprecation link and am totally fine with having moment not try to parse garbage. I found this while writing a test to make my code check for .isValid().

What is the recommended way of disabling this warning?

Contributor

elliotf commented Jun 11, 2014

Hello nice folks. I came here via the deprecation link and am totally fine with having moment not try to parse garbage. I found this while writing a test to make my code check for .isValid().

What is the recommended way of disabling this warning?

@matthew-dean

This comment has been minimized.

Show comment
Hide comment
@matthew-dean

matthew-dean Jul 6, 2014

Ugh. Postgresql native date types are no longer supported because of this?

matthew-dean commented Jul 6, 2014

Ugh. Postgresql native date types are no longer supported because of this?

@RichardJohnn

This comment has been minimized.

Show comment
Hide comment
@RichardJohnn

RichardJohnn Jul 11, 2014

@matthew-dean && @elliotf ,

moment.createFromInputFallback = function(config) {
  // unreliable string magic, or
  config._d = new Date(config._i);
}; 

via @Meekohi's comment is how to put the old behavior back in for now.

RichardJohnn commented Jul 11, 2014

@matthew-dean && @elliotf ,

moment.createFromInputFallback = function(config) {
  // unreliable string magic, or
  config._d = new Date(config._i);
}; 

via @Meekohi's comment is how to put the old behavior back in for now.

@ichernev

This comment has been minimized.

Show comment
Hide comment
@ichernev

ichernev Jul 14, 2014

Contributor

@matthew-dean can you please be more specific? What date format do you want parsed? Also if you know that this comes from postgresql I'd really suggest writing a proper format and using it when you want to parse it. This approach is far superior, and this is why we're deprecating the Date fallback in the first place.

Contributor

ichernev commented Jul 14, 2014

@matthew-dean can you please be more specific? What date format do you want parsed? Also if you know that this comes from postgresql I'd really suggest writing a proper format and using it when you want to parse it. This approach is far superior, and this is why we're deprecating the Date fallback in the first place.

@matthew-dean

This comment has been minimized.

Show comment
Hide comment
@matthew-dean

matthew-dean Jul 14, 2014

Sorry, forgot to update this. Bad form. It was neither Moment nor Postgresql date formats, but the incorrectly-written gist (of someone else) I was basing some code on that used the two.

matthew-dean commented Jul 14, 2014

Sorry, forgot to update this. Bad form. It was neither Moment nor Postgresql date formats, but the incorrectly-written gist (of someone else) I was basing some code on that used the two.

@skinnybrit51

This comment has been minimized.

Show comment
Hide comment
@skinnybrit51

skinnybrit51 Jul 17, 2014

Sorry if I have miss understood something here but I just want to be clear on how isValid() is working.

Current version moment('2014 05 12').isValid() // true + warning message
Next major version moment('2014 05 12').isValid() // false + no warning message

This correct?

skinnybrit51 commented Jul 17, 2014

Sorry if I have miss understood something here but I just want to be clear on how isValid() is working.

Current version moment('2014 05 12').isValid() // true + warning message
Next major version moment('2014 05 12').isValid() // false + no warning message

This correct?

@brunocoelho

This comment has been minimized.

Show comment
Hide comment
@brunocoelho

brunocoelho commented Sep 8, 2015

@Radagaisus What about this?

screen shot 2015-09-08 at 15 40 58

@Radagaisus

This comment has been minimized.

Show comment
Hide comment
@Radagaisus

Radagaisus Sep 8, 2015

@brunocoelho Thanks! Do you know of a way to silence it in Node.js? Our tests output are spammed with these warnings.

Radagaisus commented Sep 8, 2015

@brunocoelho Thanks! Do you know of a way to silence it in Node.js? Our tests output are spammed with these warnings.

@mixflame

This comment has been minimized.

Show comment
Hide comment
@mixflame

mixflame Sep 8, 2015

Fork the library, search the text. Disable the output. Then use your fork.
Is what I recommend.

On Tue, Sep 8, 2015 at 1:23 PM, Almog Melamed notifications@github.com
wrote:

@brunocoelho https://github.com/brunocoelho Thanks! Do you know of a
way to silence it in Node.js? Our tests output are spammed with these
warnings.


Reply to this email directly or view it on GitHub
#1407 (comment).

mixflame commented Sep 8, 2015

Fork the library, search the text. Disable the output. Then use your fork.
Is what I recommend.

On Tue, Sep 8, 2015 at 1:23 PM, Almog Melamed notifications@github.com
wrote:

@brunocoelho https://github.com/brunocoelho Thanks! Do you know of a
way to silence it in Node.js? Our tests output are spammed with these
warnings.


Reply to this email directly or view it on GitHub
#1407 (comment).

@brunocoelho

This comment has been minimized.

Show comment
Hide comment
@brunocoelho

brunocoelho Sep 8, 2015

@Radagaisus I'm afraid not, I don't use Node.js that much.

@jsilverMDX And remembering to merge with the original one every time a new version is released? I don't think it's a good idea.

brunocoelho commented Sep 8, 2015

@Radagaisus I'm afraid not, I don't use Node.js that much.

@jsilverMDX And remembering to merge with the original one every time a new version is released? I don't think it's a good idea.

stuartpb added a commit to stuartpb/stuartpb.com that referenced this issue Sep 21, 2015

@pmolaro

This comment has been minimized.

Show comment
Hide comment
@pmolaro

pmolaro Sep 24, 2015

For what its worth, I was getting the same warning and was initially confused at why I was getting it. After reading through several of the comments here and some trial and error, this seems to work for me, and I no longer get the error. So moving forward, is this the way moment intends for us to pass in dates? It seemed more useful to have it handling that for users:

return (moment(new Date(data)).isValid() ? moment(new Date(data)).format('MM/DD/YYYY h:mm A') : ' -- ');

In the example above if I have a valid date, I format that, otherwise I return 2 dashes as a placeholder. So "September, 22 2015 17:17:33 -0400" gets converted to "09/22/2015 5:17 PM"

pmolaro commented Sep 24, 2015

For what its worth, I was getting the same warning and was initially confused at why I was getting it. After reading through several of the comments here and some trial and error, this seems to work for me, and I no longer get the error. So moving forward, is this the way moment intends for us to pass in dates? It seemed more useful to have it handling that for users:

return (moment(new Date(data)).isValid() ? moment(new Date(data)).format('MM/DD/YYYY h:mm A') : ' -- ');

In the example above if I have a valid date, I format that, otherwise I return 2 dashes as a placeholder. So "September, 22 2015 17:17:33 -0400" gets converted to "09/22/2015 5:17 PM"

@cspotcode

This comment has been minimized.

Show comment
Hide comment
@cspotcode

cspotcode Sep 24, 2015

@pmolaro: Moment can do that, too. You just need to give it a format string. For example:

var d = moment(data, 'MMMM Do YYYY, h:mm:ss a'); // don't forget your format string
return (d.isValid() ? d.format('MM/DD/YYYY h:mm A') : ' -- ');

You can specify an array of formats, and moment will try them all.

cspotcode commented Sep 24, 2015

@pmolaro: Moment can do that, too. You just need to give it a format string. For example:

var d = moment(data, 'MMMM Do YYYY, h:mm:ss a'); // don't forget your format string
return (d.isValid() ? d.format('MM/DD/YYYY h:mm A') : ' -- ');

You can specify an array of formats, and moment will try them all.

@jetzhou

This comment has been minimized.

Show comment
Hide comment
@jetzhou

jetzhou Oct 6, 2015

For everyone following @cspotcode's workaround, note that the second argument should actually be moment.ISO_8601, ie

moment("invalid-date", moment.ISO_8601).isValid()==false

relating to #2036

jetzhou commented Oct 6, 2015

For everyone following @cspotcode's workaround, note that the second argument should actually be moment.ISO_8601, ie

moment("invalid-date", moment.ISO_8601).isValid()==false

relating to #2036

@auluckh23

This comment has been minimized.

Show comment
Hide comment
@auluckh23

auluckh23 Oct 7, 2015

What if my date is in this format: "2015-10-06 00:00:00.0" and in my function I am doing this:

moment("2015-10-06 00:00:00.0").format("D MMM YYYY");

Is this fine or will it stop working?

auluckh23 commented Oct 7, 2015

What if my date is in this format: "2015-10-06 00:00:00.0" and in my function I am doing this:

moment("2015-10-06 00:00:00.0").format("D MMM YYYY");

Is this fine or will it stop working?

c1phr added a commit to c1phr/bootstrap-datetimepicker that referenced this issue Oct 14, 2015

Fix for Moment deprecation warning
[Moment deprecated](moment/moment#1407)
calling ```moment()``` without formatting information, and the
defaultDate setup was calling ```getMoment()``` without ever having
fired ```initFormatting()```. This threw an ugly deprecation warning in
the console from Moment.
@nitulkukadia

This comment has been minimized.

Show comment
Hide comment
@nitulkukadia

nitulkukadia Oct 19, 2015

@ichernev Could you please add the final conclusion, As comments in this issue is too long.

nitulkukadia commented Oct 19, 2015

@ichernev Could you please add the final conclusion, As comments in this issue is too long.

@adrianaguirre

This comment has been minimized.

Show comment
Hide comment
@adrianaguirre

adrianaguirre Oct 22, 2015

I'm sorry guys I'm having a hard time understanding. How can I modify this code so that I am compliant?

var Benchmark = require('benchmark'),
    moment = require("./../moment.js"),
    base = moment('2013-05-25');

module.exports = {
  name: 'clone',
  onComplete: function(){console.log('done');},
  fn: function(){base.clone();},
  async: true
};

adrianaguirre commented Oct 22, 2015

I'm sorry guys I'm having a hard time understanding. How can I modify this code so that I am compliant?

var Benchmark = require('benchmark'),
    moment = require("./../moment.js"),
    base = moment('2013-05-25');

module.exports = {
  name: 'clone',
  onComplete: function(){console.log('done');},
  fn: function(){base.clone();},
  async: true
};
@warrendodsworth

This comment has been minimized.

Show comment
Hide comment
@warrendodsworth

warrendodsworth Oct 23, 2015

By using the default JavaScript Date constructor to construct your date object before sending it in to moment. So moment doesn't have to be responsible for parsing your Date string.

moment(new Date('2013-05-25'));

warrendodsworth commented Oct 23, 2015

By using the default JavaScript Date constructor to construct your date object before sending it in to moment. So moment doesn't have to be responsible for parsing your Date string.

moment(new Date('2013-05-25'));
@ipsita93

This comment has been minimized.

Show comment
Hide comment
@ipsita93

ipsita93 Oct 27, 2015

I get the same error as @imsobear. I am trying to convert from "9:00 AM" format to "00:00:00" format so I can store it as a TIME type in MySQL database. But how to do so?

moment("9:00 AM").format("HH:mm:ss");

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info. Error at Function.createFromInputFallback (http://localhost:3000/javascripts/vendor/moment.js:746:36) at configFromString (http://localhost:3000/javascripts/vendor/moment.js:826:32) at configFromInput (http://localhost:3000/javascripts/vendor/moment.js:1353:13) at prepareConfig (http://localhost:3000/javascripts/vendor/moment.js:1340:13) at createFromConfig (http://localhost:3000/javascripts/vendor/moment.js:1307:44) at createLocalOrUTC (http://localhost:3000/javascripts/vendor/moment.js:1385:16) at local__createLocal (http://localhost:3000/javascripts/vendor/moment.js:1389:16) at utils_hooks__hooks (http://localhost:3000/javascripts/vendor/moment.js:16:29) at Object.$.validate.submitHandler (http://localhost:3000/javascripts/addstudentform.js:620:35) at d (http://localhost:3000/javascripts/vendor/jquery.validate.min.js:4:885) addstudentform.js:621 Invalid date

ipsita93 commented Oct 27, 2015

I get the same error as @imsobear. I am trying to convert from "9:00 AM" format to "00:00:00" format so I can store it as a TIME type in MySQL database. But how to do so?

moment("9:00 AM").format("HH:mm:ss");

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info. Error at Function.createFromInputFallback (http://localhost:3000/javascripts/vendor/moment.js:746:36) at configFromString (http://localhost:3000/javascripts/vendor/moment.js:826:32) at configFromInput (http://localhost:3000/javascripts/vendor/moment.js:1353:13) at prepareConfig (http://localhost:3000/javascripts/vendor/moment.js:1340:13) at createFromConfig (http://localhost:3000/javascripts/vendor/moment.js:1307:44) at createLocalOrUTC (http://localhost:3000/javascripts/vendor/moment.js:1385:16) at local__createLocal (http://localhost:3000/javascripts/vendor/moment.js:1389:16) at utils_hooks__hooks (http://localhost:3000/javascripts/vendor/moment.js:16:29) at Object.$.validate.submitHandler (http://localhost:3000/javascripts/addstudentform.js:620:35) at d (http://localhost:3000/javascripts/vendor/jquery.validate.min.js:4:885) addstudentform.js:621 Invalid date
@iamstarkov

This comment has been minimized.

Show comment
Hide comment
@iamstarkov

iamstarkov Nov 8, 2015

I started getting deprecation warning, but cannot see how I can use both locale and strictness without using this going-to-be-deprecated feature:

moment('23 December 2015', 'DD MMMM YYYY', 'en', true).isValid();

@ichernev what is the migration path for this feature?

iamstarkov commented Nov 8, 2015

I started getting deprecation warning, but cannot see how I can use both locale and strictness without using this going-to-be-deprecated feature:

moment('23 December 2015', 'DD MMMM YYYY', 'en', true).isValid();

@ichernev what is the migration path for this feature?

@adamreisnz

This comment has been minimized.

Show comment
Hide comment
@adamreisnz

adamreisnz Nov 10, 2015

I seem to be getting this warning after a moment object is coerced into a string. The resulting string becomes %222015-11-11T10:59:59.999Z%22, which corresponds to "2015-11-11T10:59:59.999Z". Isn't a moment supposed to convert itself to a string into a format which would be validated by the moment constructor itself?

Is moment adding those quotes or is that coming out of the toString() method like that?

adamreisnz commented Nov 10, 2015

I seem to be getting this warning after a moment object is coerced into a string. The resulting string becomes %222015-11-11T10:59:59.999Z%22, which corresponds to "2015-11-11T10:59:59.999Z". Isn't a moment supposed to convert itself to a string into a format which would be validated by the moment constructor itself?

Is moment adding those quotes or is that coming out of the toString() method like that?

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@adambuczynski looks like url encoding. Moment won't do that, must be coming from elsewhere.

Member

mj1856 commented Nov 11, 2015

@adambuczynski looks like url encoding. Moment won't do that, must be coming from elsewhere.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@iamstarkov - That code is fine and does not produce the deprecation error, nor will it be removed.

Member

mj1856 commented Nov 11, 2015

@iamstarkov - That code is fine and does not produce the deprecation error, nor will it be removed.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@ipsita93

moment("9:00 AM","h:mm a").format("HH:mm:ss");
Member

mj1856 commented Nov 11, 2015

@ipsita93

moment("9:00 AM","h:mm a").format("HH:mm:ss");
@iamstarkov

This comment has been minimized.

Show comment
Hide comment
@iamstarkov

iamstarkov Nov 11, 2015

@mj1856 actually, its producing error, while im using momentjs in get-md-date in exactly that way

iamstarkov commented Nov 11, 2015

@mj1856 actually, its producing error, while im using momentjs in get-md-date in exactly that way

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@warrendodsworth - yuck - no, please avoid that. The Date constructor will parse differently depending on implementation. In most current browsers, moment(new Date('2013-05-25')) would produce the local equivalent of the UTC midnight of that date, similar to if you used moment.utc('2013-05-25').local().

Just do moment('2013-05-25'), which is not deprecated. Or, to be absolutely certain, use moment('2013-05-25','YYYY-MM-DD'), which will do the same thing.

Member

mj1856 commented Nov 11, 2015

@warrendodsworth - yuck - no, please avoid that. The Date constructor will parse differently depending on implementation. In most current browsers, moment(new Date('2013-05-25')) would produce the local equivalent of the UTC midnight of that date, similar to if you used moment.utc('2013-05-25').local().

Just do moment('2013-05-25'), which is not deprecated. Or, to be absolutely certain, use moment('2013-05-25','YYYY-MM-DD'), which will do the same thing.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@adrianaguirre - Your code is already compliant, at least the code you showed here. 2013-05-25 is one of the recognized ISO 8601 formats.

Member

mj1856 commented Nov 11, 2015

@adrianaguirre - Your code is already compliant, at least the code you showed here. 2013-05-25 is one of the recognized ISO 8601 formats.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@auluckh23 - Yes, that is fine. Fractional seconds are valid by ISO8601.

Member

mj1856 commented Nov 11, 2015

@auluckh23 - Yes, that is fine. Fractional seconds are valid by ISO8601.

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

@pmolaro, and all others trying to just remove the deprecation warning. Yes, moment(new Date(string)) will stop the deprecation warning, but then you are entirely missing the point.

The point is, parsing dates via the Date object is unreliable and inconsistent across platforms. There are different implementations and different behaviors. Sometimes you may get local time. Sometimes you may get UTC. Sometimes you may get Invalid Date. Sometimes you will get different results near DST transitions.

Don't try to defeat the error. Instead, follow our guidelines. Either use a known format or supply a format string.

The known formats are the ISO8601 forms listed here, and the older ASP.NET JSON Date form shown here. Anything else needs a format string.

Member

mj1856 commented Nov 11, 2015

@pmolaro, and all others trying to just remove the deprecation warning. Yes, moment(new Date(string)) will stop the deprecation warning, but then you are entirely missing the point.

The point is, parsing dates via the Date object is unreliable and inconsistent across platforms. There are different implementations and different behaviors. Sometimes you may get local time. Sometimes you may get UTC. Sometimes you may get Invalid Date. Sometimes you will get different results near DST transitions.

Don't try to defeat the error. Instead, follow our guidelines. Either use a known format or supply a format string.

The known formats are the ISO8601 forms listed here, and the older ASP.NET JSON Date form shown here. Anything else needs a format string.

@moment moment locked and limited conversation to collaborators Nov 11, 2015

@mj1856

This comment has been minimized.

Show comment
Hide comment
@mj1856

mj1856 Nov 11, 2015

Member

I've locked this thread. If after reading this thread and looking at your code carefully you still don't understand, then chat with me on Gitter. There's nothing more to talk about here. Thanks.

Member

mj1856 commented Nov 11, 2015

I've locked this thread. If after reading this thread and looking at your code carefully you still don't understand, then chat with me on Gitter. There's nothing more to talk about here. Thanks.

@mj1856 mj1856 removed the todo label Nov 11, 2015

santigimeno referenced this issue in santigimeno/cron-parser Feb 29, 2016

spathon referenced this issue in sequelize/sequelize Aug 24, 2017

feat(dependency): updated dependencies (#8159)
debug@3.0.0
validator@8.0.0
inflection@1.12.0
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.