From f4c2f3ceccbd5e57c2314becbf0de8b4187de5e4 Mon Sep 17 00:00:00 2001 From: Albina Date: Sun, 17 Nov 2024 13:51:23 +0100 Subject: [PATCH] Completed the task --- src/formatDate.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..f76179ff 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,32 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const separator = fromFormat[3]; + const parts = date.split(separator); + + const dateObj = {}; + + for (let i = 0; i < 3; i++) { + dateObj[fromFormat[i]] = parts[i]; + } + + if (fromFormat.includes('YYYY') && toFormat.includes('YY')) { + dateObj['YY'] = dateObj['YYYY'].slice(-2); + } + + if (fromFormat.includes('YY') && toFormat.includes('YYYY')) { + const year = parseInt(dateObj['YY'], 10); + + dateObj['YYYY'] = year < 30 ? `20${dateObj['YY']}` : `19${dateObj['YY']}`; + } + + const newSeparator = toFormat[3]; + const newDate = toFormat + .slice(0, 3) + .map((part) => dateObj[part]) + .join(newSeparator); + + return newDate; } module.exports = formatDate;