Convert number of days into years, months, days

后端 未结 6 649
轮回少年
轮回少年 2021-01-12 06:21

I have two date pickers that calculates the number of days there are between the two dates. At the moment I\'m outputting the number of days (see code below) which is kind o

6条回答
  •  一生所求
    2021-01-12 06:49

    This will give you the difference between two dates, in milliseconds

    var diff = Math.abs(date1 - date2);
    

    In your example, it'd be

    var diff = Math.abs(new Date() - compareDate);
    

    You need to make sure that compareDate is a valid Date object.

    Something like this will probably work for you

    var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
    

    i.e. turning "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend.

    Answer Reference

提交回复
热议问题