Search code examples
javascriptdayjs

Problem with calculating day difference between two dates


Hello I have a problem with calculating difference between two dates using dayjs. My code looks like this:

const isPeriodLongerThanNinetyDays = (timestamp) => {
    const dateOfFeedbackFormatted = dayjs(timestamp).format('DD/MM/YYYY');
    const actualDate = dayjs();
    const dateOfFeedback = dayjs(dateOfFeedbackFormatted);
    const dateDifference = actualDate.diff(dateOfFeedback, 'day')
    return dateDifference;
}

I'm missing out something because this method returns NaN everytime instead of number and I don't know why.


Solution

  • It looks like you tried to perform diff on a formatted date. Just delete .format('DD/MM/YYYY') If you need to format it - do it after diff

    const isPeriodLongerThanNinetyDays = (timestamp) => {
        const dateOfFeedback = dayjs(timestamp);
        const actualDate = dayjs();
        const dateDifference = actualDate.diff(dateOfFeedback, 'day')
        return dateDifference.format('DD/MM/YYYY');
    }