diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..60dc53e0 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,29 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const separator = fromFormat.pop(); + const parts = date.split(separator); + + const dateParts = {}; + + fromFormat.forEach((part, index) => { + dateParts[part] = parts[index]; + }); + + if (toFormat.includes('YY') && dateParts['YYYY']) { + dateParts['YY'] = dateParts['YYYY'].slice(-2); + } + + if (toFormat.includes('YYYY') && dateParts['YY']) { + dateParts['YYYY'] = + dateParts['YY'] < '30' ? '20' + dateParts['YY'] : '19' + dateParts['YY']; + } + + const newSeparator = toFormat.pop(); + + const newDate = toFormat.map((part) => dateParts[part]).join(newSeparator); + + return newDate; } module.exports = formatDate;