美文网首页
js计算天数对应年月日

js计算天数对应年月日

作者: 水君子Z | 来源:发表于2018-10-26 15:08 被阅读0次

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

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

相关文章

网友评论

      本文标题:js计算天数对应年月日

      本文链接:https://www.haomeiwen.com/subject/rknktqtx.html