Skip to content

Commit

Permalink
make formateDate
Browse files Browse the repository at this point in the history
  • Loading branch information
kepchuk1234 committed Jan 17, 2025
1 parent 67725ad commit fdfa743
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,65 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const result = [];
let year = '';
let month = '';
let day = '';
const spliter = fromFormat[fromFormat.length - 1];
const joiner = toFormat[toFormat.length - 1];
const numbers = date.split(spliter);

for (let i = 0; i < fromFormat.length; i++) {
switch (fromFormat[i]) {
case 'YYYY':
year = numbers[i];
break;

case 'YY':
year = numbers[i];
break;

case 'MM':
month = numbers[i];
break;

case 'DD':
day = numbers[i];
break;
}
}

for (const type of toFormat) {
switch (type) {
case 'YYYY':
if (type.length > year.length) {
if (year < 30) {
year = '20' + year;
} else {
year = '19' + year;
}
}
result.push(year);
break;

case 'YY':
if (type.length < year.length) {
year = year.slice(2);
}
result.push(year);
break;

case 'MM':
result.push(month);
break;

case 'DD':
result.push(day);
break;
}
}

return result.join(joiner);
}

module.exports = formatDate;

0 comments on commit fdfa743

Please sign in to comment.