diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..91da5cee 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -7,8 +7,52 @@ * * @returns {string} */ -function formatDate(date, fromFormat, toFormat) { - // write code here -} +function formatDate(date, fromDate, toDate) { + const indexDd = fromDate.indexOf('DD'); + const indexMm = fromDate.indexOf('MM'); + const indexYyyy = fromDate.indexOf('YYYY'); + const indexYy = fromDate.indexOf('YY'); + + const dateArr = date.split(fromDate[3]); + + const dd = dateArr[indexDd]; + const mm = dateArr[indexMm]; + let yyyy = dateArr[indexYyyy]; + let yy = dateArr[indexYy]; + + if (fromDate.indexOf('YYYY') === -1) { + if (yy < 30) { + yyyy = '20' + yy; + } else { + yyyy = '19' + yy; + } + } else { + yy = yyyy.slice(-2); + } + let newFormatDate = ''; + + for (let i = 0; i < toDate.length - 1; i++) { + switch (toDate[i]) { + case 'DD': + newFormatDate += dd; + break; + case 'MM': + newFormatDate += mm; + break; + case 'YYYY': + newFormatDate += yyyy; + break; + case 'YY': + newFormatDate += yy; + break; + } + + if (i !== toDate.length - 2) { + newFormatDate += toDate[3]; + } + } + + return newFormatDate; +} module.exports = formatDate;