Friendly Date Ranges

題目:
  題目給定一個 YYYY-MM-DD格式的日期,Y:Year、M:Month、D:Day,年份4個digit、月份2個digit、幾日2個digit,Ex: 發文日以這個格式則為: 2016-09-27,題目要我們把給定日期區間轉換成比較容易閱讀的格式,規則如下:
  不要顯示那些可以被推測出來的信息: 如果一個日期區間裏結束日期與開始日期相差不超一年,則結束日期就不用寫年份了. 月份開始和結束日期如果在同一個月,則結束日期月份就不用寫了
另外, 如果日期區間內開始日期年份是當前年份,且結束日期與開始不超過一年,則開始日期的年份也不用寫.
  幾日部分:1號⇒1st、2號⇒2nd、3號⇒3nd、其他:
day < 10 ⇒ 05 → 5th
day > 10 ⇒ Ex: 10 → 10th
  以下範例:
makeFriendlyDates(["2016-07-01", "2016-07-04"]) 應該返回 ["July 1st","4th"] makeFriendlyDates(["2016-07-01", "2018-07-04"]) 應該返回 ["July 1st, 2016", "July 4th, 2018"].
function makeFriendlyDates(arr) {
  var month = {
    1 : "January", 2 : "February", 3 : "March", 4 : "April",
    5 : "May", 6 : "June", 7 : "July", 8 : "August",
    9 : "September", 10 : "October", 11 : "November", 12 : "December",
  };
  /*
  var startDate = arr[0].split("-");
  var endDate = arr[1].split("-");
  var start = new Date(parseInt(startDate[0]), parseInt(startDate[1] - 1), parseInt(startDate[2]));
  var end = new Date(parseInt(endDate[0]), parseInt(endDate[1] -1), parseInt(endDate[2]));
  */
  var start = new Date(arr[0]);
  var end = new Date(arr[1]);
  var diff = (end - start) / (1000  * 60  * 60 * 24 );

  if(diff === 0)
    return [month[start.getMonth() + 1] + " " + dayTrans(start.getDate()) + ", " + start.getFullYear()];
  else if(start.getFullYear() === end.getFullYear())
  {
    if(start.getMonth() === end.getMonth() )
      return [month[start.getMonth() + 1] + " " + dayTrans(start.getDate()) , dayTrans(end.getDate()) ];
    else if( start.getMonth() != end.getMonth())
      return [ month[start.getMonth() + 1] + " " + dayTrans(start.getDate()) + ", " + start.getFullYear(), month[end.getMonth() +1] + " " + dayTrans(end.getDate())] ;
  }
  else if(diff < 365)
  {
    if(start.getMonth() === end.getMonth() )
      return [ month[start.getMonth() +1] + " " + dayTrans(start.getDate()) + ", " + start.getFullYear(), month[end.getMonth() +1] + " " + dayTrans(end.getDate())];
    else if(start.getFullYear() != end.getFullYear())
      return [month[start.getMonth() +1 ] + " " + dayTrans(start.getDate()), month[end.getMonth() + 1] + " " + dayTrans(end.getDate()) ] ;
  }
  else if( diff >= 365)
    return [month[start.getMonth() +1] + " " + dayTrans(start.getDate()) + ", " + start.getFullYear(), month[end.getMonth() +1] + " " + dayTrans(end.getDate()) + ", " + end.getFullYear()] ;//["December 1st, 2016","February 3rd, 2018"].


}

function dayTrans(day)
{
    var result;
    switch(day)
    {
      case 1:
        result = "1st";
        break;
      case 2:
        result = "2nd";
        break;
      case 3:
        result = "3rd";
        break;
      default:
        if(day < 10)
            result = day + "th";
        else
            result = day + "th";
        break;
    }
    return result;
}

makeFriendlyDates(['2016-07-01', '2016-07-04']);
makeFriendlyDates(["2022-09-05", "2023-09-05"]);
makeFriendlyDates(["2018-01-13", "2018-01-13"]);
makeFriendlyDates(["2016-12-01", "2018-02-03"]);
makeFriendlyDates(["2017-03-01", "2017-05-05"]);
makeFriendlyDates(["2022-09-05", "2023-09-04"]); 
makeFriendlyDates(["2016-12-01", "2017-02-03"]);

測試的正確結果

makeFriendlyDates(["2016-07-01", "2016-07-04"]) should return ["July 1st","4th"].
makeFriendlyDates(["2016-12-01", "2017-02-03"]) should return ["December 1st","February 3rd"].
makeFriendlyDates(["2016-12-01", "2018-02-03"]) should return ["December 1st, 2016","February 3rd, 2018"].
makeFriendlyDates(["2017-03-01", "2017-05-05"]) should return ["March 1st, 2017","May 5th"]
makeFriendlyDates(["2018-01-13", "2018-01-13"]) should return ["January 13th, 2018"].
makeFriendlyDates(["2022-09-05", "2023-09-04"]) should return ["September 5th, 2022","September 4th"].
makeFriendlyDates(["2022-09-05", "2023-09-05"]) should return ["September 5th, 2022","September 5th, 2023"].

Solution:

  題目要求我們把日期區間轉換成比較容易閱讀的格式,我們先使用split把年月日個欄位分出來,然後用Date Object產生Date 並將開始跟結束時間相減,得到時間間距,再依據題目要求,如果年份一樣不寫結束日期的年份,月份一樣不寫結束月份,我把它分為4部分,Ⅰ.同一天的話時間間距(diff)就是0 Ⅱ.時間區間在同一年中同一個月與不同月的狀況 Ⅲ.再來就是時間間距小於1年中,開始時間與結束時間為同一個月和開始日期與結束日期為不同年的狀況 Ⅳ.時間間距大於一年的狀況,分別返回不同的字串,使用Date Object中的getFullYear()、getMonth()+1[Range: 0 ~ 11]、getDate()這幾個函數。
下面是Date Object一些用法:
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)

//Date對象構造函數(建構子)參數說明
//milliseconds - 距離JavaScript內部定義的起始時間1970年1月1日的毫秒數
//datestring - 字符串代表的日期與時間。此字符串可以使用Date.parse()轉換
//year - 四位數的年份,如果取值為0-99,則在其之上加上1900
//month - 0(代表一月)-11(代表十二月)之間的月份
//day - 1-31之間的日期
//hours - 0(代表午夜)-23之間的小時數
//minutes - 0-59之間的分鐘數
//seconds - 0-59之間的秒數
//microseconds - 0-999之間的毫秒數
var date = new Date("2009-08-08);
//也可以用這種格式作為參數



**P.S. / Reference:  String.split()
           String.substr()
           parseInt()
           JavaScript Date 对象与函数

results matching ""

    No results matching ""