From a26a49aa27d03ac0d569c51a393f11c56a7b5e81 Mon Sep 17 00:00:00 2001 From: Egor Mamtsev Date: Tue, 7 Jan 2025 11:17:22 -0600 Subject: [PATCH] done --- src/formatDate.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..4a74fc09 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,56 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const separator = fromFormat[fromFormat.length - 1]; + const splittedDate = date.split(separator); + + const result = []; + + for (let i = 0; i < toFormat.length - 1; i++) { + const firstLetter = toFormat[i][0]; + + for (const el of fromFormat) { + if (el.includes(firstLetter)) { + const index = fromFormat.indexOf(el); + + result[i] = splittedDate[index]; + } + } + } + + for (const el of toFormat) { + if (el.includes('Y') && el.length === 2) { + const index = toFormat.indexOf(el); + + if (result[index].length === 2) { + return result.join(toFormat[toFormat.length - 1]); + } + + if (result[index].length === 4) { + result[index] = result[index].slice(-2); + + return result.join(toFormat[toFormat.length - 1]); + } + } + + if (el.includes('Y') && el.length === 4) { + const index = toFormat.indexOf(el); + + if (result[index].length === 2) { + if (+result[index] < 30) { + result[index] = 20 + result[index]; + + return result.join(toFormat[toFormat.length - 1]); + } else { + result[index] = 19 + result[index]; + + return result.join(toFormat[toFormat.length - 1]); + } + } + } + } + + return result.join(toFormat[toFormat.length - 1]); } module.exports = formatDate;