Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,51 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const separator = fromFormat.at(-1);
const dateParts = date.split(separator);

const yearIndex = fromFormat.includes('YYYY')
? fromFormat.indexOf('YYYY')
: fromFormat.indexOf('YY');
let year = dateParts[yearIndex];

const monthIndex = fromFormat.indexOf('MM');
const month = dateParts[monthIndex];

const dayIndex = fromFormat.indexOf('DD');
const day = dateParts[dayIndex];

if (year.length === 2) {
if (Number(year) < 30) {
year = '20' + year;
} else {
year = '19' + year;
}
}

if (year.length === 4 && toFormat.includes('YY')) {
year = year.slice(-2);
}

const resultParts = [];

for (let i = 0; i < 3; i++) {
if (toFormat[i] === 'DD') {
resultParts.push(day);
}

if (toFormat[i] === 'MM') {
resultParts.push(month);
}

if (toFormat[i] === 'YYYY' || toFormat[i] === 'YY') {
resultParts.push(year);
}
}

const newSeparator = toFormat.at(-1);

return resultParts.join(newSeparator);
}

module.exports = formatDate;
Loading