Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
solyaqw committed Nov 18, 2024
1 parent 67725ad commit 3434d5d
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 separator = fromFormat.pop();
const parts = date.split(separator);

const dateParts = {};

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

if (toFormat.includes('YY') && dateParts['YYYY']) {
dateParts['YY'] = dateParts['YYYY'].slice(-2);
}

if (toFormat.includes('YYYY') && dateParts['YY']) {
dateParts['YYYY'] =
dateParts['YY'] < '30' ? '20' + dateParts['YY'] : '19' + dateParts['YY'];
}

const newSeparator = toFormat.pop();

const newDate = toFormat.map((part) => dateParts[part]).join(newSeparator);

return newDate;
}

module.exports = formatDate;

0 comments on commit 3434d5d

Please sign in to comment.