27-ой день программы "#100 Days Of Code"
Convert a date range consisting of two dates formatted as YYYY-MM-DD
into a more readable format.
The friendly display should use month names instead of numbers and ordinal dates instead of cardinal (1st
instead of 1
).
Do not display information that is redundant or that can be inferred by the user: if the date range ends in less than a year from when it begins, do not display the ending year.
Additionally, if the date range begins in the current year (i.e. it is currently the year 2016) and ends within one year, the year should not be displayed at the beginning of the friendly range.
If the range ends in the same month that it begins, do not display the ending year or month.
Examples:
makeFriendlyDates(["2016-07-01", "2016-07-04"])
should return ["July 1st","4th"]
makeFriendlyDates(["2016-07-01", "2018-07-04"])
should return ["July 1st, 2016", "July 4th, 2018"]
.
function makeFriendlyDates(str) { var months = ['January','February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; function convertDate(str) { var dateStr = str.split('-'); return (new Date(Date.UTC(dateStr[0], dateStr[1] - 1, dateStr[2]))); } var dateA = convertDate(str[0]); var dateB = convertDate(str[1]); function dayEnd (val){ if(val==1 || val ==21 || val == 31){ return val +'st'; }else if(val == 2 || val == 22){ return val + 'nd'; } else if(val == 3 || val == 33){ return val +'rd'; }else{ return val + 'th'; } } function monthDiff(dateA, dateB) { var monthB = dateB.getUTCFullYear() * 12 + dateB.getUTCMonth(); var monthA = dateA.getUTCFullYear() * 12 + dateA.getUTCMonth(); return monthB - monthA; } function dayDiff(dateA, dateB) { if(dateB.getUTCMonth() === dateA.getUTCMonth()){ return dateA.getUTCDate()-dateB.getUTCDate(); } return 0; } function getMonth(date) { return months[date.getUTCMonth()]; } function displayDate() { if (dateB.getTime() - dateA.getTime() === 0) { return [getMonth(dateA) + ' ' + dayEnd(dateA.getUTCDate()) + ', ' + dateA.getUTCFullYear()]; } if (dateA.getUTCMonth() === dateB.getUTCMonth() && dateA.getUTCFullYear() === dateB.getUTCFullYear()) { return [getMonth(dateA) + ' ' + dayEnd(dateA.getUTCDate()), dayEnd(dateB.getUTCDate())]; } if (monthDiff(dateA, dateB) < 12 && dateA.getUTCFullYear() !== dateB.getUTCFullYear() ) { return [getMonth(dateA) + ' ' + dayEnd(dateA.getUTCDate()), getMonth(dateB) + ' ' + dayEnd(dateB.getUTCDate())]; } if (monthDiff(dateA, dateB) <= 12 && dayDiff(dateA, dateB)>0) { return [getMonth(dateA) + ' ' + dayEnd(dateA.getUTCDate())+', '+dateA.getUTCFullYear(), getMonth(dateB) + ' ' + dayEnd(dateB.getUTCDate())]; } if (monthDiff(dateA, dateB) < 12) { return [getMonth(dateA) + ' ' + dayEnd(dateA.getUTCDate())+', '+dateA.getUTCFullYear(), getMonth(dateB) + ' ' + dayEnd(dateB.getUTCDate())]; } return [getMonth(dateA) + ' ' + dayEnd(dateA.getUTCDate()) + ', ' + dateA.getUTCFullYear(), getMonth(dateB) + ' ' + dayEnd(dateB.getUTCDate()) + ', ' + dateB.getUTCFullYear()]; } return displayDate(); } makeFriendlyDates(['2016-07-01', '2016-07-04']);
Ответы:
makeFriendlyDates(["2016-07-01", "2016-07-04"])
return ["July 1st","4th"].
makeFriendlyDates(["2016-12-01", "2017-02-03"])
return ["December 1st","February 3rd"].
makeFriendlyDates(["2016-12-01", "2018-02-03"])
return ["December 1st, 2016","February 3rd, 2018"].
makeFriendlyDates(["2017-03-01", "2017-05-05"])
return ["March 1st, 2017","May 5th"]
makeFriendlyDates(["2018-01-13", "2018-01-13"])
return ["January 13th, 2018"].
makeFriendlyDates(["2022-09-05", "2023-09-04"])
return ["September 5th, 2022","September 4th"].
makeFriendlyDates(["2022-09-05", "2023-09-05"])
return ["September 5th, 2022","September 5th, 2023"].
Комментариев нет:
Отправить комментарий