From f18a728feddf09a45a493aeb7a815a362f35a127 Mon Sep 17 00:00:00 2001 From: Kateryna Berdnikova Date: Mon, 1 Jun 2026 17:28:43 +0300 Subject: [PATCH] solution --- src/formatDate.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e27661..e185c5043 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,39 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here + const dateParts = date.split(fromFormat.at(-1)); + + const day = dateParts[fromFormat.indexOf('DD')]; + const month = dateParts[fromFormat.indexOf('MM')]; + const year = + dateParts[fromFormat.indexOf('YYYY')] || + dateParts[fromFormat.indexOf('YY')]; + + return toFormat + .map((format) => { + switch (format) { + case 'DD': + return day; + case 'MM': + return month; + + case 'YYYY': + if (year < 100 && year.slice(-2) < 30) { + return '20' + year.slice(-2); + } else if (year < 100) { + return '19' + year.slice(-2); + } else { + return year; + } + + case 'YY': + return year.slice(-2); + default: + return ''; + } + }) + .slice(0, -1) + .join(toFormat.at(-1)); } module.exports = formatDate;