From 34b0c5e1acfbfde78d007a8210154f02f62aadcc Mon Sep 17 00:00:00 2001 From: Anna Skobara Date: Wed, 3 Jun 2026 12:56:27 +0300 Subject: [PATCH] Solution --- src/formatDate.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e27661..5a0d6a714 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,51 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const separator = fromFormat.at(-1); + const dateParts = date.split(separator); + + const yearIndex = fromFormat.includes('YYYY') + ? fromFormat.indexOf('YYYY') + : fromFormat.indexOf('YY'); + let year = dateParts[yearIndex]; + + const monthIndex = fromFormat.indexOf('MM'); + const month = dateParts[monthIndex]; + + const dayIndex = fromFormat.indexOf('DD'); + const day = dateParts[dayIndex]; + + if (year.length === 2) { + if (Number(year) < 30) { + year = '20' + year; + } else { + year = '19' + year; + } + } + + if (year.length === 4 && toFormat.includes('YY')) { + year = year.slice(-2); + } + + const resultParts = []; + + for (let i = 0; i < 3; i++) { + if (toFormat[i] === 'DD') { + resultParts.push(day); + } + + if (toFormat[i] === 'MM') { + resultParts.push(month); + } + + if (toFormat[i] === 'YYYY' || toFormat[i] === 'YY') { + resultParts.push(year); + } + } + + const newSeparator = toFormat.at(-1); + + return resultParts.join(newSeparator); } module.exports = formatDate;