Skip to content

Commit

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

for (let i = 0; i < dateParts.length; i++) {
dateSet[fromFormat[i]] = dateParts[i];
}

const newFormat = [];

for (const key of toFormat) {
if (dateSet[key]) {
newFormat.push(dateSet[key]);
} else if (key === 'YY') {
newFormat.push(dateSet.YYYY.slice(2));
} else if (key === 'YYYY') {
if (dateSet.YY < 30) {
newFormat.push('20' + dateSet.YY);
} else {
newFormat.push('19' + dateSet.YY);
}
}
}

return newFormat.join(toSeparator);
}

module.exports = formatDate;

0 comments on commit 97ff2c2

Please sign in to comment.