From 75b8ca17c9a51aa6df4bbf9162a6af8bcda1923b Mon Sep 17 00:00:00 2001 From: Daria Chakhalian Date: Sat, 30 May 2026 15:38:43 +0300 Subject: [PATCH 1/2] Solution --- src/formatDate.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e27661..4d5ec2780 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,41 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const fromSeparator = fromFormat[fromFormat.length - 1]; + const toSeparator = toFormat[toFormat.length - 1]; + const dateParts = date.split(fromSeparator); + const obj = {}; + + for (let i = 0; i <= 2; i++) { + obj[fromFormat[i]] = dateParts[i]; + } + + if ('YYYY' in obj && toFormat[0].length === toFormat[2].length) { + obj['YYYY'] = obj['YYYY'].slice(-2); + } else if (+obj['YY'] < 30) { + obj['YY'] = '20' + obj['YY']; + } else { + obj['YY'] = '19' + obj['YY']; + } + + const toDateParts = []; + + for (let i = 0; i <= 2; i++) { + switch (toFormat[i]) { + case 'DD': + toDateParts[i] = obj['DD']; + break; + case 'MM': + toDateParts[i] = obj['MM']; + break; + case 'YYYY': + case 'YY': + toDateParts[i] = obj['YYYY'] || obj['YY']; + break; + } + } + + return toDateParts.join(toSeparator); } module.exports = formatDate; From fce1ba2376b369c6be11be0c3dfced16dbb52099 Mon Sep 17 00:00:00 2001 From: Daria Chakhalian Date: Sat, 30 May 2026 19:02:02 +0300 Subject: [PATCH 2/2] fixed --- src/formatDate.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/formatDate.js b/src/formatDate.js index 4d5ec2780..b32f66304 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -17,12 +17,12 @@ function formatDate(date, fromFormat, toFormat) { obj[fromFormat[i]] = dateParts[i]; } - if ('YYYY' in obj && toFormat[0].length === toFormat[2].length) { + if (fromFormat.includes('YYYY') && toFormat.includes('YY')) { obj['YYYY'] = obj['YYYY'].slice(-2); - } else if (+obj['YY'] < 30) { - obj['YY'] = '20' + obj['YY']; - } else { - obj['YY'] = '19' + obj['YY']; + } + + if (fromFormat.includes('YY') && toFormat.includes('YYYY')) { + obj['YY'] = Number(obj['YY'] < 30) ? '20' + obj['YY'] : '19' + obj['YY']; } const toDateParts = [];