Skip to content

Commit

Permalink
create new format for dates
Browse files Browse the repository at this point in the history
  • Loading branch information
VvynnykV committed Apr 5, 2024
1 parent 456bf4f commit 50ddd47
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 oldDivider = fromFormat[3];
const newDivider = toFormat[3];
const dateParts = date.split(oldDivider);
const dateData = {};
const newFormat = [];

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

if (dateData.hasOwnProperty('YYYY')) {
dateData.YY = dateData.YYYY.slice(2);
}

if (dateData.YY < 30) {
dateData.YYYY = '20' + dateData.YY;
} else {
dateData.YYYY = '19' + dateData.YY;
}

for (let i = 0; i < toFormat.length - 1; i++) {
newFormat.push(dateData[toFormat[i]]);
}

return newFormat.join(newDivider);
}

module.exports = formatDate;

0 comments on commit 50ddd47

Please sign in to comment.