Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Allatiun committed Feb 7, 2025
1 parent 67725ad commit 2909123
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,41 @@
*
* @returns {string}
*/

function formatDate(date, fromFormat, toFormat) {
// write code here
}
const fromSep = fromFormat[3];
const toSep = toFormat[3];

// Розбиваємо рядок за роздільником
const parts = date.split(fromSep);

// Створюємо об'єкт для відповідності частин дати
const dateParts = {};

fromFormat.slice(0, 3).forEach((part, index) => {
dateParts[part] = parts[index];
});

// Обробка формату року
if ('YYYY' in dateParts && toFormat.includes('YY')) {
dateParts['YY'] = dateParts['YYYY'].slice(2);
} else if ('YY' in dateParts && toFormat.includes('YYYY')) {
const year = parseInt(dateParts['YY'], 10);

if (year === 0) {
dateParts['YYYY'] = '2000';
} else if (year < 30) {
dateParts['YYYY'] = '20' + dateParts['YY'];
} else {
dateParts['YYYY'] = '19' + dateParts['YY'];
}
}

// Формуємо вихідний рядок у новому форматі
return [
dateParts[toFormat[0]],
dateParts[toFormat[1]],
dateParts[toFormat[2]],
].join(toSep);
}
module.exports = formatDate;

0 comments on commit 2909123

Please sign in to comment.