Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanVaniuk committed Dec 2, 2024
1 parent 67725ad commit ab176dd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,29 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const fromSeparator = fromFormat.pop();
const toSeparator = toFormat.pop();
const dateParts = date.split(fromSeparator);

const mapping = {};

for (let i = 0; i < fromFormat.length; i++) {
const key = fromFormat[i];

mapping[key] = dateParts[i];
}

if (fromFormat.includes('YYYY') && toFormat.includes('YY')) {
mapping['YY'] = mapping['YYYY'].slice(-2);
} else if (fromFormat.includes('YY') && toFormat.includes('YYYY')) {
const year = parseInt(mapping['YY'], 10);

mapping['YYYY'] = year < 30 ? `20${mapping['YY']}` : `19${mapping['YY']}`;
}

const result = toFormat.map((part) => mapping[part]).join(toSeparator);

return result;
}

module.exports = formatDate;

0 comments on commit ab176dd

Please sign in to comment.