Convert number of days into years, months, days

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

    Considering
    1 Year => 365 Days
    1 MONTH => 30 Days
    1 WEEK => 7 Days
    value => NUMBER OF DAYS
    
    function parseDays (value) 
            { 
               var year, months, week, days;
             
               year = value >= 365 ? Math.floor(value / 365) : 0;
               value = year ? value - (year*365) : value;
            
               months = value >= 30 ? Math.floor((value % 365) / 30) : 0;
               value = months ? value - (months*30) : value;
            
               week = value >= 7 ? Math.floor((value % 365) / 7) : 0;
               value = week ? value - (week*7) : value;
            
               days = value < 7 ? Math.floor((value % 365) % 7) : 0;
            
               console.log("years = ", year); 
               console.log("months = ",months); 
               console.log("weeks = ",week); 
               console.log("days = ", days);     
            }
    

提交回复
热议问题