From 17925568aee37a0431fa0dc2a910feb727d2c47b Mon Sep 17 00:00:00 2001 From: jkumorek01 Date: Wed, 1 Jul 2026 15:01:26 +0200 Subject: [PATCH 1/3] First task solution --- src/formatDate.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e27661..8698a7c01 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,45 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const changeElement = toFormat[toFormat.length - 1]; + const parts = date.split(baseElement); + + const fromDay = fromFormat.indexOf('DD'); + const fromMonth = fromFormat.indexOf('MM'); + + const fromYearIndex = fromFormat.includes('YYYY') + ? fromFormat.indexOf('YYYY') + : fromFormat.indexOf('YY'); + + const day = parts[fromDay]; + const month = parts[fromMonth]; + let year = parts[fromYearIndex]; + + if (fromFormat.includes('YYYY') && toFormat.includes('YY')) { + year = year.slice(2); + } + + if (fromFormat.includes('YY') && toFormat.includes('YYYY')) { + year = Number(year) < 30 ? `20${year}` : `19${year}`; + } + + const result = []; + + for (const element of toFormat.slice(0, 3)) { + if (element === 'DD') { + result.push(day); + } + + if (element === 'MM') { + result.push(month); + } + + if (element === 'YY' || element === 'YYYY') { + result.push(year); + } + } + + return result.join(changeElement); } module.exports = formatDate; From 08adfb54f3f955d76312d96a8e09b862ea904ad8 Mon Sep 17 00:00:00 2001 From: jkumorek01 Date: Wed, 1 Jul 2026 15:05:58 +0200 Subject: [PATCH 2/3] Second task solution --- src/formatDate.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/formatDate.js b/src/formatDate.js index 8698a7c01..157766d2b 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,6 +8,7 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { + const baseElement = fromFormat[toFormat.length - 1]; const changeElement = toFormat[toFormat.length - 1]; const parts = date.split(baseElement); From fd3d6626a3fa8ae71714af7e14c019131efd9025 Mon Sep 17 00:00:00 2001 From: jkumorek01 Date: Wed, 1 Jul 2026 15:54:35 +0200 Subject: [PATCH 3/3] Third task solution --- src/formatDate.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/formatDate.js b/src/formatDate.js index 157766d2b..9d8a629b4 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,9 +8,9 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - const baseElement = fromFormat[toFormat.length - 1]; - const changeElement = toFormat[toFormat.length - 1]; - const parts = date.split(baseElement); + const inputSeparator = fromFormat[fromFormat.length - 1]; + const outputSeparator = toFormat[toFormat.length - 1]; + const parts = date.split(inputSeparator); const fromDay = fromFormat.indexOf('DD'); const fromMonth = fromFormat.indexOf('MM'); @@ -47,7 +47,7 @@ function formatDate(date, fromFormat, toFormat) { } } - return result.join(changeElement); + return result.join(outputSeparator); } module.exports = formatDate;