接收天数,返回对应格式:
function conversion(day){
if(day < 30){
return day +'天'
}else if(day < 365){
let month = Math.floor(day/30);
let days = Math.round((day/30-month)*30)
if(days == 0){
return month + '个月'
}else{
return month + '个月'+ days +'天'
}
}else{
let year = Math.floor(day/365);
let month = Math.floor((day/365 - year)*365/30);
let days = Math.round(((day/365 - year)*365/30 - month)*30)
if(month == 0 && days == 0){
return year+'年'
}else if(days == 0){
return year+'年'+month+'个月'
}else{
return year+'年'+month+'个月'+days+'天'
}
}
}
var result = conversion(365*2+60+5)
console.log(result)//2年2个月5天