Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
vikapazyuk committed Dec 17, 2024
1 parent 67725ad commit 969276c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,31 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const fromSeparator = fromFormat[3];
const toSeparator = toFormat[3];

// Розбиваємо вхідну дату за роздільником
const dateParts = date.split(fromSeparator);

// Створюємо мапу для зручного доступу до частин дати
const dateMap = {};

for (let i = 0; i < 3; i++) {
dateMap[fromFormat[i]] = dateParts[i];
}

// Обробка року (перетворення між YY та YYYY)
if (fromFormat.includes('YY') && toFormat.includes('YYYY')) {
dateMap['YYYY'] = (dateMap['YY'] < 30 ? '20' : '19') + dateMap['YY'];
} else if (fromFormat.includes('YYYY') && toFormat.includes('YY')) {
dateMap['YY'] = dateMap['YYYY'].slice(-2);
}

// Формуємо новий формат
const newDateParts = toFormat.slice(0, 3).map((part) => dateMap[part]);

// Повертаємо результат з новим роздільником
return newDateParts.join(toSeparator);
}

module.exports = formatDate;

0 comments on commit 969276c

Please sign in to comment.