js计算天数对应年月日

2018-10-26  本文已影响0人  水君子Z

接收天数,返回对应格式:

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天
控制台运行情况
上一篇 下一篇

猜你喜欢

热点阅读