From 3434d5d546ede7d465637f24fdd040c408b30087 Mon Sep 17 00:00:00 2001 From: Solomiia Palka Date: Mon, 18 Nov 2024 10:13:59 +0200 Subject: [PATCH 1/2] Solution --- src/formatDate.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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; From 9ed8fbbb8c311dd0ed842f7ccb65cfd5e39ce2e7 Mon Sep 17 00:00:00 2001 From: Solomiia Palka Date: Mon, 18 Nov 2024 10:26:49 +0200 Subject: [PATCH 2/2] add copys --- src/formatDate.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/formatDate.js b/src/formatDate.js index 60dc53e0..5db84324 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,27 +8,32 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - const separator = fromFormat.pop(); + const fromFormatCopy = [...fromFormat]; + const toFormatCopy = [...toFormat]; + + const separator = fromFormatCopy.pop(); const parts = date.split(separator); const dateParts = {}; - fromFormat.forEach((part, index) => { + fromFormatCopy.forEach((part, index) => { dateParts[part] = parts[index]; }); - if (toFormat.includes('YY') && dateParts['YYYY']) { + if (toFormatCopy.includes('YY') && dateParts['YYYY']) { dateParts['YY'] = dateParts['YYYY'].slice(-2); } - if (toFormat.includes('YYYY') && dateParts['YY']) { + if (toFormatCopy.includes('YYYY') && dateParts['YY']) { dateParts['YYYY'] = dateParts['YY'] < '30' ? '20' + dateParts['YY'] : '19' + dateParts['YY']; } - const newSeparator = toFormat.pop(); + const newSeparator = toFormatCopy.pop(); - const newDate = toFormat.map((part) => dateParts[part]).join(newSeparator); + const newDate = toFormatCopy + .map((part) => dateParts[part]) + .join(newSeparator); return newDate; }