Convert number of days into years, months, days

后端 未结 6 805
轮回少年
轮回少年 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 07:07

    When you want to get the year and month between two date:

         var dateFrom = '2017-08-10'; 
         var dateTo ='2019-04-23';
         var date1 = new Date(dateFrom);
         var date2 = new Date(dateTo);
         var diff=0;
         var month=31;
         var days=1000*60*60*24;
         diff=date2-date1; 
         var day=(Math.floor(diff/days));   
         var years = (Math.floor(day/365));
         var months = Math.round(day % 365)/month;
        document.write(years+"year-"+months);
    

提交回复
热议问题