const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" (dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days").format(DATE_FORMAT);
const currentDate = moment().startOf("day").format(DATE_FORMAT);
const validDate = moment(currentDate).isSameOrBefore(appValidDate);
I have been trying to pare two dates using moment. While running the application, i am getting the below deprecation warning.
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 uping major release. Please refer to / for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Invalid date, _f: undefined, _strict: undefined, _locale: [object Object]
Error:
Found some usefult stackoverflow links: Moment.js deprecation warning when paring two dates
But still not able to remove the deprecation warning.
So as per the doc, need to be in string + format, so i have done like this:
const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" (dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days").format(DATE_FORMAT);
const currentDate = moment().startOf("day").format(DATE_FORMAT);
const validDate = moment(currentDate, DATE_FORMAT).isSameOrBefore(appValidDate);
But the problem is we can't convert endDate to string & then subtract the days. If i am passing like that, getting Moment error.
Can anybody help me to find a proper solution for this. Any help would be really appreciated.
const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" (dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days").format(DATE_FORMAT);
const currentDate = moment().startOf("day").format(DATE_FORMAT);
const validDate = moment(currentDate).isSameOrBefore(appValidDate);
I have been trying to pare two dates using moment. While running the application, i am getting the below deprecation warning.
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 uping major release. Please refer to http://momentjs./guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Invalid date, _f: undefined, _strict: undefined, _locale: [object Object]
Error:
Found some usefult stackoverflow links: Moment.js deprecation warning when paring two dates
But still not able to remove the deprecation warning.
So as per the doc, need to be in string + format, so i have done like this:
const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" (dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days").format(DATE_FORMAT);
const currentDate = moment().startOf("day").format(DATE_FORMAT);
const validDate = moment(currentDate, DATE_FORMAT).isSameOrBefore(appValidDate);
But the problem is we can't convert endDate to string & then subtract the days. If i am passing like that, getting Moment error.
Can anybody help me to find a proper solution for this. Any help would be really appreciated.
Share Improve this question asked May 6, 2020 at 2:41 VishnuVishnu 72416 silver badges39 bronze badges 5-
You have
moment(currentDate)
. Look at the value you're assigning tocurrentDate
. I would just remove both calls to.format(DATE_FORMAT)
since you're not displaying those dates and they're best left asmoment
instances – Phil Commented May 6, 2020 at 2:56 - @Phil Yes somewhere i am displaying appValidDate in code. Requirement is like endDate is in the above format (finnish date). Compare those values and display in YYY-MM-DD format how can i convert currentDate to endDate? – Vishnu Commented May 6, 2020 at 3:03
-
Format it when you display it, not before. When you're doing date arithmetic, work directly with the
moment
instances – Phil Commented May 6, 2020 at 3:04 - @Phil Problem is how can i convert currentDate to endDate format? endDate is finnish Date. – Vishnu Commented May 6, 2020 at 3:08
- If you supply as string that is not one of the recognised formats, moment.js falls back to the built-in parser, which is notoriously unreliable. Dates in the format "YYYY-MM-DD" will be parsed as UTC, but other methods work as local so startOf("day") will be the start of the local day, which may not coincide with the start of the UTC day. – RobG Commented May 6, 2020 at 7:38
1 Answer
Reset to default 5As explained in the ments above, do your date parison using moment
instances.
The value returned by .format()
is a string and depending on the format chosen (and possibly your locale) may trigger the warning you're seeing.
Use .format()
when you want to display a value.
const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" //(dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days");
const currentDate = moment().startOf("day");
// or for a UTC "start of day"
// const currentDate = moment.utc().startOf('day')
const validDate = currentDate.isSameOrBefore(appValidDate);
console.log('appValidDate:', appValidDate.format(DATE_FORMAT))
console.log('currentDate:', currentDate.format(DATE_FORMAT))
console.log('validDate:', validDate)
<script src="https://momentjs./downloads/moment.js"></script>