-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Comments
+1. I totally agree. We should start issuing a deprecation warning right away. Some additional support:
|
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 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. |
…r to moment/moment#1407 of constructing new moment using strings
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 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 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
The warning is very much for you :) |
This is implemented, so I'm closing it. |
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 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 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. |
@icambron Is the correct way to plugin your own parser to overwrite moment.createFromInputFallback = function(config) {
// unreliable string magic, or
config._d = new Date(config._i);
}; |
@Meekohi Correct. |
hi guys,
now when i check log on my server, i get error message on my log files
|
@Aredo I think you're expecting |
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? |
(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). |
@icambron Got it thanks, I'll make sure to do so! |
@icambron When i try this |
@imsobear I'm not sure the error message you see is from |
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.js deprecation warning and related issues: moment/moment#1407
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 What is the recommended way of disabling this warning? |
@pmolaro: Moment can do that, too. You just need to give it a format string. For example:
You can specify an array of formats, and moment will try them all. |
For everyone following @cspotcode's workaround, note that the second argument should actually be
relating to #2036 |
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? |
@ichernev Could you please add the final conclusion, As comments in this issue is too long. |
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
}; |
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.
|
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//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 |
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? |
I seem to be getting this warning after a moment object is coerced into a string. The resulting string becomes Is moment adding those quotes or is that coming out of the toString() method like that? |
@adambuczynski looks like url encoding. Moment won't do that, must be coming from elsewhere. |
@iamstarkov - That code is fine and does not produce the deprecation error, nor will it be removed. |
moment("9:00 AM","h:mm a").format("HH:mm:ss"); |
@mj1856 actually, its producing error, while im using momentjs in |
@warrendodsworth - yuck - no, please avoid that. The Just do |
@adrianaguirre - Your code is already compliant, at least the code you showed here. |
@auluckh23 - Yes, that is fine. Fractional seconds are valid by ISO8601. |
@pmolaro, and all others trying to just remove the deprecation warning. Yes, The point is, parsing dates via the 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. |
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. |
debug@3.0.0 validator@8.0.0 inflection@1.12.0
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 dobut you can't do reliably (therefore its being deprecated)
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"))
, butmoment
won't be responsible for such convertions. Read below for the rationale about the changeAs 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
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.
The text was updated successfully, but these errors were encountered: