Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii-Bidiak committed Apr 29, 2024
1 parent ac335bd commit 8323e3b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,33 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const fromSeparate = fromFormat[3];
const toSeparate = toFormat[3];
const fromFormatDate = fromFormat.slice(0, 3);
const toFormatDate = toFormat.slice(0, 3);

const origDate = date.split(fromSeparate);
const origDateObj = {};
const returnDate = {};

for (let i = 0; i < fromFormatDate.length; i++) {
origDateObj[fromFormatDate[i]] = origDate[i];
}

if ('YYYY' in origDateObj) {
origDateObj['YY'] = origDateObj['YYYY'].slice(2);
} else if ('YY' in origDateObj) {
origDateObj['YYYY'] =
origDateObj['YY'] < 30
? `20${origDateObj['YY']}`
: `19${origDateObj['YY']}`;
}

for (let i = 0; i < toFormatDate.length; i++) {
returnDate[toFormatDate[i]] = origDateObj[toFormatDate[i]];
}

return Object.values(returnDate).join(toSeparate);
}

module.exports = formatDate;

0 comments on commit 8323e3b

Please sign in to comment.