momentjs deprecated warning not going away - javascript

somestringvar = moment(entry.expirationDate, "MM/DD/YYYY").format("MMM-DD-YYYY").toString();
The date is correctly converted to the format I need like Nov-11-2018 but I just cannot get rid of that infamous moment js deprecated warning. Tried many combinations. The warning I get is
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
Please advise how to get rid of this warning. I am using latest angular version and typescript code. Thanks

A simple way to get around is changing the moment constructor to moment(new Date(entry.expirationDate)).

Related

JS offset of a date on specific browser/configuration

I've an API that returns birthdate as 2020-11-24T00:00:00 to a React app. That react app uses that portion to display it:
new Date("2020-11-24T00:00:00").toLocaleDateString();
The issue is that on my browser and all browsers I have seems to give no issue: date is correclty shown. One or two customer complains about ir because they see the date 23/11/2020 (one dat before). I cannot reproduce the bug.
As I understood, Dates can be interpreted by browser as Zulu date so browser can translate the date from GMT+0 to browser's region. Right. Now I have to try to reproduce the bug in order to fix it and And I simply cannot because of misunderstanding.
First postulate: Date("2020-11-24T00:00:00") is going wrong, let's try that: fiddle => no, I cannot reproduce with my browser
Second postulate: .toLocaleDateString() is going wrong, let's try that: [fiddle][2] => no, I cannot reproduce with my browser when changing Location in Chrome.
How can I reproduce the issue in order to fix it?
The current ECMAScript standard obliges your example date string to be interpreted as a local date/time, because the time part is included and the timezone part is not.
Through time the rules for parsing strings have become a bit more standardized, but older JavaScript engines may behave differently. See for instance a post from 2013 or 2017 where a difference was reported (at that time). It is likely that some of your users run that JavaScript on much older engines which interpret this type of date string as UTC (as if suffixed by "Z").
Mozilla Contributors write about using Date.parse (which uses the same parser as when the string is passed to the constructor):
Note: Parsing of strings with Date.parse is strongly discouraged due to browser differences and inconsistencies.
To remove all ambiguity also for older browsers, parse the string yourself:
var dateStr = "2020-11-24T00:00:00";
var dateArr = dateStr.match(/\d+/g);
var date = new Date(dateArr[0], dateArr[1]-1, dateArr[2]); // Add time parts if needed
// Guaranteed to be reporting on the 24th of November 2020 (in locale format)
console.log(date.toLocaleDateString());

Today's date: Moment construction falls back to js Date

I want today's date like: 2017/06/23
I am doing this:
moment().format('YYYY/MM/DD'))
It gives me error:
Deprecation warning: moment construction falls back to js Date. This
is discouraged and will be removed in upcoming major release.
Tried many solutions from SO and also Github issue, still nothing is working out. Please help me out of this.
Pass a new Date() without parameters to moment object.
console.log(moment(new Date()).format('YYYY/MM/DD'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>

MomentJs output Date of toDate() is incorrect

I've started using momentJs in an Angular/Typescript project. (Included incase it's relevant in any way although I very much doubt it)
In the run method of my module I call
moment.locale(window.navigator.language);
which correctly sets the locale to en-GB in my instance. Further down the line I use moment to parse a GB time.
when doing the following:
var mom = moment("24/11/2015 00:00:00");
for example. This populates a new moment object using the defaults set on the moment global (If i understand how it should work correctly). moms date is set to 2016-12-11T00:00:00.000Z. This clearly means it's parsed the given string in en-US instead of en-GB which was set via Locale in a default setting prior to this call. Is there anything I've missed in configuration/setup of moment which would make this not work?
I've also inspected the _locale property of my variable. mom._locale is set to en-gb and I can see the L,LL,LLL etc etc formats are all en-GB formatted values (as they should be).
running mom.toDate(); unsurprizingly returns the 2016 date stored internally by the moment object.
Some misc information I forgot to include:
I am using the latest release of momentjs from NuGet (Version 2.10.6 at time of writing) and I've included moment-with-locales.js in my HTML
Using any recent version of MomentJS, you should see why in the console:
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.
Unless you specify a format string, MomentJS relies on the Date object's parsing, and unfortunately, regardless of locale the Date object will, with a string using /, assume U.S. format. One of the many, many things that aren't quite right with Date.
You'll need to use a format string, or supply the string in the simplified ISO-8601 format used by Date. From Parse > String:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, then fall back to new Date(string) if a known format is not found.
var day = moment("1995-12-25");
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
So I got around this by fetching the locale data from moment and just passing it into the format parameter. Considering the example input of "24/11/2015 00:00:00" I would structure my format as below:
var format = moment.localeData().longDateFormat('L') + " " + moment.localeData().longDateFormat("LTS");
this generates the format mask of "DD/MM/YYYY HH:mm:ss".
You can mix and match whatever formats you want and this will be locale specific to whatever you set moment.locale("") to be (presuming you have the locale information setup in moment already)
This is a crazy workaround and I'm surprised that moment doesn't presume locale information as default when parsing. TJCrowder has raised an issue on Github with the moment guys which I suggest anyone who cares should comment on. https://github.com/moment/moment/issues/2770
You're probably better off passing the format to moment directly and validating the string before hand. This will ultimately reduce the amount of debugging you'll need to do and get you up and running straight away.
var mom = moment("24/11/2015 00:00:00", "DD/MM/YYYY HH:mm:ss");
You could try the new(ish) Intl API but browser support is limited (IE11+), so I would recommend having a user select the month in a dropdown or something to force them to input a certain way.

Converting simple date/time string in moment.js doesn't detect AM/PM

I'm trying to convert a simple string to a unix timestamp using moment.js
moment('2014-01-14 07:25 PM').unix();
moment('2014-01-14 07:25 AM').unix();
The problem is I get the same result with AM or PM in that string.
1389684300
What gives?
The docs make no mention that your specified format is guaranteed to be correctly recognized. It says
Warning: Browser support for this is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.`
You should probably explicitly specify a format in the second argument.
This should work (JSFiddle):
moment('2014-01-14 07:25 PM', 'YYYY-MM-DD hh:mm A').unix();
It seems to have been a bug in version 2.0.0. Updating to 2.5.0 and the problem is fixed.

In Moment.js, why doesn't subtract by 'week' work?

I'm using Moment.js and following line of code doesn't seem to actually do anything:
moment().subtract('week', 1)
In the documentation, it shows an example of subtract being used with 'days' but it doesn't specify that it's the only string you can use. In most of the other functions, it's an option where you can use 'days', 'weeks', 'months', so I'm not sure if I'm doing something wrong or 'weeks' just isn't supported:
Moment.js Subtract Documentation
Here's the example of subtracting days:
moment().subtract('days', 7);
It's also what I ended up using instead of 'weeks' but I'm still curious why 'weeks' aren't supported.
You have it backwards from the Moment API.
moment().subtract(1, 'week');
This is a bit old question but it appears that even back then it was relevant.
Definetely nowadays is even more relevant to consider parse.com moment.js version (1.7.2) to the current moment.js version (2.8.4) and the only API doc that parse.com makes reference to
Check an answer to this in a previous post in Trouble using the Moment module
I would suggest to work always with latest moment.js so you can properly work with the provided documentation (during my search I was not able to find 1.7.2 documentation.. beside you will miss many great features working with the version provided in parse.com.
Add the new version as indicated in the same post in the accepted answer.

Categories

Resources