From 5a80aa522a3a297b647f9ded817c04a057f2f22a Mon Sep 17 00:00:00 2001 From: troleksii Date: Wed, 11 Dec 2024 22:24:26 +0100 Subject: [PATCH] Solution --- src/formatDate.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..99e62e1f 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,31 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const [fromParts, separator] = [fromFormat.slice(0, -1), fromFormat[3]]; + const [toParts, separatorNew] = [toFormat.slice(0, -1), toFormat[3]]; + const dateParts = date.split(separator); + const dateFrom = {}; + const dateTo = []; + + for (let i = 0; i < fromParts.length; i++) { + dateFrom[fromParts[i]] = dateParts[i]; + } + + if (dateFrom.YYYY) { + dateFrom.YY = dateFrom.YYYY.slice(-2); + } else if (dateFrom.YY) { + if (dateFrom.YY < 30) { + dateFrom.YYYY = `20${dateFrom.YY}`; + } else { + dateFrom.YYYY = `19${dateFrom.YY}`; + } + } + + for (let i = 0; i < toParts.length; i++) { + dateTo.push(dateFrom[toParts[i]]); + } + + return dateTo.join(separatorNew); } module.exports = formatDate;