-3

I'm trying to get how many days left till the date, I'm using momentJs, below is the code I'm using but in front end it gives me Nan which means error.

const start = moment();
  const end = moment(coupon.validTill, 'DD MM YYYY');
  const diff = end.diff(start, "days")

coupon.validTill is 30/02/2023 The image below is from my collection in mongodb which shows the validity date

enter image description here

sultan.h
  • 331
  • 1
  • 4
  • 16
  • _"coupon.validTill is 30/02/2023"_ - then why are you saying that the format you wanted to parse was `DD MM YYYY`? – CBroe Nov 28 '22 at 09:08
  • Why use a library that has been [depreciated](https://momentjscom.readthedocs.io/en/latest/moment/-project-status/00-intro/) for over 2 years for a calculation that can be done with plain JavaScript? [How to calculate number of days between two dates?](https://stackoverflow.com/questions/542938/how-to-calculate-number-of-days-between-two-dates) – Yogi Nov 28 '22 at 09:13

1 Answers1

1

your validTill date is wrong, fab month can't have 30 days. use a valid date and try, it will work fine.

NAZIR HUSSAIN
  • 578
  • 1
  • 18