From 2addec8a3e5f9817e09d636f706063fd8350b8af Mon Sep 17 00:00:00 2001 From: Maksym Date: Fri, 10 Jan 2025 09:59:03 +0200 Subject: [PATCH] add solution --- src/formatDate.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..1d4bb543 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,28 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const separatorFrom = fromFormat.pop(); + const separatorTo = toFormat.pop(); + + const dateParts = {}; + const dateSegments = date.split(separatorFrom); + + fromFormat.forEach((part, index) => { + dateParts[part] = dateSegments[index]; + }); + + if (dateParts['YYYY'] && dateParts['YY'] === undefined) { + dateParts['YY'] = dateParts['YYYY'].slice(-2); + } else if (dateParts['YY'] && dateParts['YYYY'] === undefined) { + const year = parseInt(dateParts['YY'], 10); + + dateParts['YYYY'] = + year < 30 ? `20${dateParts['YY']}` : `19${dateParts['YY']}`; + } + + const result = toFormat.map((part) => dateParts[part]).join(separatorTo); + + return result; } module.exports = formatDate;