diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..465ed0e6 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -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;