FCC(ES6写法) Friendly Date Ranges

把常见的日期格式如:YYYY-MM-DD 转换成一种更易读的格式。

易读格式应该是用月份名称代替月份数字,用序数词代替数字来表示天 (1st 代替 1)。

包含当前年份和相同月份的时候,makeFriendlyDates(["2017-01-02", "2017-01-05"]) 应该返回 ["January 2nd","5th"]。

不包含当前年份,makeFriendlyDates(["2003-08-15", "2009-09-21"]) 应该返回 ["August 15th, 2003", "September 21st, 2009"]

请考虑清楚所有可能出现的情况,包括传入的日期区间是否合理。对于不合理的日期区间,直接返回 undefined 即可。

思路:

就是几个条件判断一下就行了。写的有点繁琐,有更简便的请留言。

let makeFriendlyDates = arr => {
  const 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"  };
  let date1 = arr[0].split("-");
  let date2 = arr[1].split("-");
  let date = new Date();
  let dateChange = day => {
    if(day[0] === "0"){
      day = day.substr(1);
      if(day === "1") return day + "st";  //01->1st
      if(day === "2") return day + "nd";  //02->2nd
      if(day === "3") return day + "rd";  //03->3rd
      else return day + "th";
    }
    else{
      if(day.substr(1,1) === "1" && day.substr(0,1) === "2") return day + "st";  //21->21st
      if(day.substr(1,1) === "1" && day.substr(0,1) === "3") return day + "st";  //31->31st
      if(day.substr(1,1) === "2" && day.substr(0,1) === "2") return day + "nd";  //22->22nd
      if(day.substr(1,1) === "3" && day.substr(0,1) === "2") return day + "rd";  //23->23rd
      else return day + "th";
    }
  };
  let sameYear = (d1, d2) => {
    if(d2[0] - d1[0] > 1) return false;
    else{
      if(d1[0] === d2[0]) return true;
      else{  //判断相减为1的时候
        if(d2[1] > d1[1]) return false;
        if(d2[1] < d1[1]) return true; //判断在一年以内返回true
        else return d2[2] < d1[2] ? true : false; //判断是否在一年以内
      }
    }
  };
  if (sameYear(date1, date2)) {
    if(date1[0] === date2[0]){
      if(date1[1] === date2[1]){  //月份相同
        if(date1[2] === date2[2]){  //日期相同
          let dateArr = [];
          dateArr.push(months[date1[1]] + " " + dateChange(date1[2]) + ", " + date1[0]);
          return dateArr;
        }
        else{
          let dateArr = [];
          dateArr.push(months[date1[1]] + " " + dateChange(date1[2]));
          dateArr.push(dateChange(date2[2]));
          return dateArr;
        }
      }
      else{  //月份不同
        let dateArr = [];
        dateArr.push(months[date1[1]] + " " + dateChange(date1[2]));
        dateArr.push(months[date2[1]] + " " + dateChange(date2[2]));
        return dateArr;
      }
    }
    if(date1[0] == date.getFullYear() - 1){  //开始年份为当前年份
      let dateArr = [];
      dateArr.push(months[date1[1]] + " " + dateChange(date1[2]));
      dateArr.push(months[date2[1]] + " " + dateChange(date2[2]));
      return dateArr;
    }
    else{
      let dateArr = [];
      dateArr.push(months[date1[1]] + " " + dateChange(date1[2]) + ", " + date1[0]);
      dateArr.push(months[date2[1]] + " " + dateChange(date2[2]));
      return dateArr;
    }
  }
  if (date2[0] > date1[0]){  //不同年且d2比d1大时
    let dateArr = [];
    dateArr.push(months[date1[1]] + " " + dateChange(date1[2]) + ", " + date1[0]);
    dateArr.push(months[date2[1]] + " " + dateChange(date2[2]) + ", " + date2[0]);
    return dateArr;
  }
}
makeFriendlyDates(["2017-02-05", "2017-03-03"]);

  

如果有不明白的地方请留言,如果有更好更简便更优化的方法请留言,谢谢。

更多内容请访问我的个人博客:Bblog

上一篇:java 网络编程基础 TCP/IP协议:服务端ServerSocket;客户端Socket; 采用多线程方式处理网络请求


下一篇:hdu1013