I am trying to compare a moment object (30 days ago) to a given timestamp. Both isBefore and isAfter are returning false. Im not sure where i'm going wrong?
var startdate = moment.utc().subtract(30,'days')
var RequestedDate = moment.utc('14/12/2021, 11:26')
var isbefore = startdate.isBefore(RequestedDate)
var isafter = startdate.isAfter(RequestedDate)
console.log(isafter)
console.log(isbefore)
The requested date is not in the right format, based on the error emitted in a fiddle of your code:
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. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
If you're able to use the "Date + Format" constructor, then you can parse something like this:
var RequestedDate = moment.utc('14/12/2021, 11:26', 'D/M/Y, H:m', true)
// "Tue Dec 14 2021 11:26:00 GMT+0000"
Note the use of strict mode, as in the docs:
You may get unexpected results when parsing both date and time. The below example may not parse as you expect:
moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss");
Related
I am trying to format date variable and pass it to eonasdan datetimepicker for angular but no matter what I try to do I get
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.
Arguments:
below are my tryouts:
date from ajax - Wed, 05 Jul 2017 00:00:00 GMT
/*example1*/
angular.forEach(tickets, function(value, key){
$scope.startDate = new Date(value.start_date);
$scope.endDate = new Date(value.end_date);
});
/*example2*/
angular.forEach(tickets, function(value, key){
$scope.startDate = moment(startDate, "YYYY-MM-DD");
$scope.endDate = moment(value.end_date, "YYYY-MM-DD");
});
here are datetimepicker options
$scope.calendarWidgetOptions = {
format: 'YYYY-MM-DD',
minDate: moment().startOf('d')
};
I tried other stuff but can not make it work, and get rid of deprecation warning hence date is now visible in some browsers/timezones.
The implied question is 'Why am I getting this deprecation warning', and the answer is because you're making use of the moment function in a way that's deprecated and no longer officially supported.
From the link you provided:
"...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
You're calling moment like this:
moment(value.end_date, "YYYY-MM-DD");
Which doesn't conform exactly to one of the strict signatures listed above, and so will be handled by the fuzzy handler that tries to resolve whatever value is passed in and return something usable, which is the functionality that's been deprecated.
You may be able to resolve this simply by matching the formatter exactly:
moment(value.end_date, "YYYY MM DD");
However you're also passing a Date object into moment, when it looks like it's expecting a String, so what you most likely want is to convert the Date to a string and then pass that and the correct formatter to moment.
moment(value.end_date.toISOString(), "YYYY MM DD");
or
moment(value.end_date.toISOString());
This will effectively submit something like this:
moment("2017-07-05T17:22:49.396Z", "YYYY MM DD");
or
moment("2017-07-05T17:22:49.396Z");
And this does match the expected signature and shouldn't throw the warning.
I have this format date:
01Mar-0234
26Feb-0430
01 is day, mar or feb is mounth and 0430 is 4 oclock 30 in formater zulu +00.
I would like to use moment for converting this format, I'm trying this:
moment('26Feb-0430').format("DD-MM-YY HH:MM");
but I haven't good format and I have this error :
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment c
onstruction falls back to js Date(), which is not reliable across all browsers and version
s. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major r
elease. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
Can you help me for have 26/02/2017 06:30 for summer and 5h30 winter?
As suggested in the comments and in the warning message you have to use moment(String, String) parsing function.
In the format string parameter you have to use moment tokens where: DD is day of the month. MMM is month's short name, HH is 0-23 hours and mm (lowercase) is minutes.
Since you have to threat your input as +00:00, you have to use moment.utc.
Use format() to display the parsed moment object passing the tokens you need, always remember that moment tokens are case sensitive.
Here a working sample:
var result = moment.utc('26Feb-0430', 'DDMMM-HH:mm').format("DD/MM/YYYY HH:mm");
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I am using the following code to convert a server-side date-time to local time using moment.js.
moment(moment('Wed, 23 Apr 2014 09:54:51 +0000').format('lll')).fromNow()
But I am getting:
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.
It seems I cannot get rid of it! How can I fix it?
To get rid of the warning, you need to either:
Pass in an ISO formatted version of your date string:
moment('2014-04-23T09:54:51');
Pass in the string you have now, but tell Moment what format the string is in:
moment('Wed, 23 Apr 2014 09:54:51 +0000', 'ddd, DD MMM YYYY HH:mm:ss ZZ');
Convert your string to a JavaScript Date object and then pass that into Moment:
moment(new Date('Wed, 23 Apr 2014 09:54:51 +0000'));
The last option is a built-in fallback that Moment supports for now, with the deprecated console warning. They say they won't support this fallback in future releases. They explain that using new Date('my date') is too unpredictable.
As an alternative, you can suppress showing the deprecation warning by setting moment.suppressDeprecationWarnings = true;
The date construction in moment internally uses the new Date() in the javascript. The new Date() construction recognizes the date string in either RFC2822 or ISO formats in all browsers. When constructing a moment object with date not in these formats, the deprecation warning is thrown.
Though the deprecation warnings are thrown, for some formats, the moment object will be successfully constructed in Chrome, but not in Firefox or Safari. Due to this, processing the date in Chrome may give results as expected(not all the time) and throws Invalid Date in others.
Consider, 02.02.2018,
Chrome - moment("02.02.2018")._d -> Fri Feb 02 2018 00:00:00 GMT+0530 (India Standard Time)
Firefox - moment("02.02.2018")._d -> Invalid Date
Safari - moment("02.02.2018")._d -> Invalid Date
So the moment.js is used at your own risk in case the recommended/standard formats are not used.
To suppress the deprecation warnings,
As suggested by #Joe Wilson in previous answer, give the date format on moment construction.
Example : moment("02.05.2018", "DD.MM.YYYY").format("DD MM YYYY");
Give the date in ISO or RFC2822 format.
Example : moment("2018-02-01T18:30:00.000Z") - ISO Format
moment("Thu, 01 Feb 2018 18:30:00 GMT") - RFC2822 Format - Format in Github
As suggested by #niutech in previous answer, set
moment.suppressDeprecationWarnings = true;
I suggest to overwrite the input fallback in moment.
moment.createFromInputFallback=function (config){
config._d = new Date(config._i);
}
As (3) will suppress all the warnings, (4) will suppress only the date construction fallback. Using (4), you will get Invalid Date as the internal new Date() is used and other deprecations can be seen in console, so moment can be upgraded or the deprecated methods can be replaced in the application.
If your date is passed to you from an API as string(like my issue), you can use a filter to convert the string to a date for moment. This will take care of the moment construction warning.
$scope.apiDate = 10/29/2017 18:28:03";
angular.module('myApp').filter('stringToDate', function() {
return function(value) {
return Date.parse(value);
};
});
Add it to the view:
{{apiDate | stringToDate | amDateFormat:'ddd, MMM DD'}}
In my case I was trying to generate a date time to include in my form data. But the format of the string I had access to looked like "10-Sep-2020 10:10" so when trying to use moment like
myDate = '10-Sep-2020 10:10';
moment(myDate).format('YYYY-MM-DD HH:mm:ss');
I got the deprecation warning. There is no problem using a string to create the date but you just have to let moment know what it is you are passing in. As in explicitly state the format it is about to receive, for example
moment(myDate, 'DD-MMM-YYYY HH:mm').format('YYYY-MM-DD HH:mm:ss');
result: 2020-09-10 10:10:00
That's it, the warning goes away, moment is happy and you have a date time format ready for persistence.
As indicated in the above answers. Providing the date format should work.
Why would I be getting the deprecation message with the following line of code. I thought the String + format was suppose to remedy the issue. moment.tz('2015:08:20 14:33:20', 'YYYY:MM:DD HH:mm:ss', 'America/New_York'). Also, please not I do not have control over the date format being provide. I know I can convert it myself to 'YYYY-MM-DDTHH:mm:ss' then moment does not show the deprecation message. However, according to the documentation, the line of code should work. Here is the deprecation message I am seeing.
"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."
Moment’s usage is so widespread that it’s impossible to deprecate the current version over time. Check out this post on alternative options Migrating away from moment.js
I'm trying to parse the date which is in the following format
dateFormat: "d-M-y" // returns 10-Oct-13
I'm using jQuery UI for formatting date.
Below is my code:
var d1 = new Date("10-Oct-13");
alert(d1); //Invalid in FF and IE, works in chrome
Seems weird, here is my JSFiddle for reproducing the bug in FF and IE.
Note: I don't want to use plugin, since it is working chrome.
Please share your thoughts.
You can use Datepicker's parseDate() method in conjunction with the format string to parse the date:
var d1 = $.datepicker.parseDate("d-M-y", $("#lastAssimilationDate").val())
alert(d1); // alerts: Thu Oct 10 2013 00:00:00 GMT+0200
See the edited JSFiddle.
From the MDN doc for Date:
dateString
String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).
Essentially you're passing a string in an unsupported date format as the dateString parameter of the constructor, so the JavaScript engine is (correctly) stating that it's an invalid date. Chrome seems to be slightly more forgiving with the date formats it allows, but that's non-standard.
You can use the getDate function to obtain a Date object representing your selected date:
var d1 = $('#lastAssimilationDate').datepicker("getDate");
Use the built-in getDate method:
$('button').click(function(){
var d1 = $("#lastAssimilationDate" ).datepicker('getDate');
console.log(d1);
});
You can also assign an altField with an altFormat of yyyy-mm-dd if you need to send an ISO-standard date to the server.
var startDate = new Date('2013-05-13');
var date_format = d3.time.format("%m/%d");
if I do
startDate = date_format(startDate);
I get "05/12" instead of "05/13". Anyone has a clue why is this happening?
Don’t use the Date(string) constructor to parse dates; it varies from browser to browser. The most likely (but not guaranteed) interpretation of "2013-05-13" is as an ISO8601 string in UTC time. Thus, if you run your statement in the JavaScript console, you will see:
> new Date("2013-05-13")
Sun May 12 2013 17:00:00 GMT-0700 (PDT)
The string is interpreted in UTC time by the Date construct, while the returned Date object is in local time. May 13, midnight UTC is May 12 5PM PDT, so when you format it in local time using d3.time.format, you get back May 12.
You could switch to using d3.time.format.utc("%m/%d") to format your date, but then you’re still dependent on the ambiguous behavior of the Date(string) constructor. So, instead…
As #minikomi suggested, you could create a d3.time.format to parse a date string: d3.time.format("%Y-%m-%d"), then format.parse("2013-05-13"). Or you could use the multi-argument Date constructor: new Date(2013, 4, 13), but note that months start at zero rather than the usual one.
You might get more consistent results using a d3.time.format to parse your string as well:
var startDate = '2013-05-13';
var parser = d3.time.format("%Y-%m-%d");
var formatter = d3.time.format("%m/%d");
var startDateString = formatter(parser.parse(startDate));