FCC Friendly Date Ranges

直接上代码

function makeFriendlyDates(arr) {
  var months = {
    "01" : "January",
    "02" : "February",
    "03" : "March",
    "04" : "April",
    "05" : "May",
    "06" : "June",
    "07" : "July",
    "08" : "August",
    "09" : "September",
    "10" : "October",
    "11" : "November",
    "12" : "December",
  };
  
  //长度为3的数组,分别为年份,月份,日
  var date1 = arr[0].split("-");//第一个日期
  var date2 = arr[1].split("-");//第二个日期
  var date = new Date();
  //日转换函数
  function dateParse(day) {
    if(day[0] === "0"){
      day = day.substr(1);
      if(day === "1"){
        return day + "st";
      }
      if(day === "2"){
        return day + "nd";
      }
      if(day === "3"){
        return day + "rd";
      }
      else{
        return day + "th";
      }
    }
    else{
      if(day.substr(1,1) === "1" && day.substr(0,1) === "2"){
        return day + "st";
      }
      if(day.substr(1,1) === "1" && day.substr(0,1) === "3"){
        return day + "st";
      }
      if(day.substr(1,1) === "2" && day.substr(0,1) === "2"){
        return day + "nd";
      }
      if(day.substr(1,1) === "3" && day.substr(0,1) === "2"){
        return day + "rd";
      }
      else{
        return day + "th";
      }
    }
    
  }
         
  //判断是否在一年以内
  function isOneYear(date1, date2) {
    if(date2[0] - date1[0] > 1){
      return false;
    }
    else{
      if(date1[0] === date2[0]){//年份相等
        return true;
      }
      else{
        if(date2[1] > date1[1]) {
          return false;
        }
        if(date2[1] < date1[1]) {
          return true;
        }
        else{
          if(date2[2] < date1[2]){
            return true;
          }
          else{
            return false;
          }
        }
      }
    }
  }
  
  
  //在一年之内
  if (isOneYear(date1, date2)) {
    
    //年份相同
    if(date1[0] === date2[0]){
      
      //月份相同
      if(date1[1] === date2[1]){
                            
        //日期相同
        if(date1[2] === date2[2]){
          let dateArr = [];
          dateArr.push(months[date1[1]] + " " + dateParse(date1[2]) + ", " + date1[0]);
          return dateArr;
        }
        
        //日期不同
        else{
          let dateArr = [];
          dateArr.push(months[date1[1]] + " " + dateParse(date1[2]));
          dateArr.push(dateParse(date2[2]));
          return dateArr;
        }
        
      }
      
      //月份不同
      else{
        let dateArr = [];
          dateArr.push(months[date1[1]] + " " + dateParse(date1[2]));
          dateArr.push(months[date2[1]] + " " + dateParse(date2[2]));
          return dateArr;
      }
    }
    
    //开始年份为当前年份
    if(date1[0] == date.getFullYear() - 1){
      let dateArr = [];
      dateArr.push(months[date1[1]] + " " + dateParse(date1[2]));
      dateArr.push(months[date2[1]] + " " + dateParse(date2[2]));
      return dateArr;
    }
    
    //年份不同
    else{
      let dateArr = [];
      dateArr.push(months[date1[1]] + " " + dateParse(date1[2]) + ", " + date1[0]);
      dateArr.push(months[date2[1]] + " " + dateParse(date2[2]));
      return dateArr;
    }
 }  
  
  
  //不在一年以内
  if (date2[0] > date1[0]){
    let dateArr = [];
    dateArr.push(months[date1[1]] + " " + dateParse(date1[2]) + ", " + date1[0]);
    dateArr.push(months[date2[1]] + " " + dateParse(date2[2]) + ", " + date2[0]);
    return dateArr;
  }
 
}
makeFriendlyDates(["2017-02-01", "2017-03-03"]);

你可能感兴趣的:(js,array)