Friendly Date Ranges(freecodecamp高级算法5)

让日期区间更友好!

把常见的日期格式如: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 即可

function makeFriendlyDates(arr) {
                    var currYear = (new Date()).getFullYear();//2017,当前年份
                
                    var fir = arr[0].split("-");
                    var las = arr[1].split("-");
            var firStr,lasStr; var arrStr = [];//返回数组 var month = ["January","February","March","April","May","June","July","August","September" ,"October","November","December"]; firStr = month[fir[1]-1] + " " + changeDate(fir[2]) + ", " + fir[0]; //是否同一年 var flag = (las[0] == fir[0]) || ((las[0] == Number(fir[0])+1) && (fir[1] > las[1])) || ((las[0] == Number(fir[0])+1) && (fir[1] == las[1]) && (fir[2] > las[2])); if(las[0] == fir[0] && las[1] == fir[1] && las[2] == fir[2]){//结束日期和开始日期相同,返回的结束日期为空 lasStr = ""; }else if(las[0] == fir[0] && las[1] == fir[1] ){ //如果一个日期区间里结束日期与开始日期相差小于一年,则结束日期就不用写年份了; //在这种情况下,如果月份开始和结束日期如果在同一个月,则结束日期月份也不用写了。 lasStr = changeDate(las[2]); }else if(flag){//同一年,结束日期不写年 lasStr = month[las[1]-1] + " " + changeDate(las[2]); }else{ lasStr = month[las[1]-1] + " " + changeDate(las[2]) + ", " + las[0]; } if(fir[0] == currYear && flag){//如果开始日期年份是当前年份,且结束日期与开始日期小于一年,则开始日期的年份也不用写。 firStr = firStr.substr(0,firStr.length-6);//如:", 2017" } arrStr[0] = firStr; if(lasStr != ""){ arrStr[1] = lasStr; } if(fir[0] > las[0]){ return undefined; } return arrStr; } function changeDate(str){//返回对应日期 var nub = Number(str);//"01"-->1;也可以使用parseInt(str) switch(nub){ case 1: case 21: case 31: nub += "st";break; case 2: case 22: nub += "nd";break; case 3: case 23: nub += "rd";break; default: nub += "th";break; } return nub; }
         //alert(makeFriendlyDates(['2017-01-02', '2017-01-05']));//["January 2nd","5th"]. //alert(makeFriendlyDates(["2017-02-01", "2017-03-03"]));//["February 1st","March 3rd"]. //alert(makeFriendlyDates(["2016-05-11", "2017-04-04"]));//["May 11th, 2016","April 4th"]. //alert(makeFriendlyDates(["2017-07-12", "2018-06-13"]));//["July 12th","June 13th"] alert(makeFriendlyDates(["2016-05-11", "2017-04-04"]));//["May 11th, 2016","April 4th"]. alert(makeFriendlyDates(["2017-07-12", "2018-06-13"]));//["July 12th","June 13th"] alert(makeFriendlyDates(["2003-08-15", "2009-09-21"]));//["August 15th, 2003", "September 21st, 2009"]. alert(makeFriendlyDates(["2010-10-23", "2011-10-22"]));//["October 23rd, 2010","October 22nd"]. alert(makeFriendlyDates(["2008-10-31", "2009-10-31"]));//["October 31st, 2008","October 31st, 2009"]. alert(makeFriendlyDates(["2004-11-17", "2005-12-25"]));//["November 17th, 2004","December 25th, 2005"]. alert(makeFriendlyDates(["2001-12-20", "2001-12-20"]));//["December 20th, 2001"].

 

posted @ 2017-08-29 15:40  随风而逝XXX  Views(201)  Comments(0Edit  收藏  举报