Skip to content

Commit

Permalink
add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
PolianskyiDmytro committed Jan 7, 2025
1 parent 67725ad commit bccde19
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
47 changes: 45 additions & 2 deletions src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,50 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
}
const fromSpliter = fromFormat[fromFormat.length - 1];
const toSpliter = toFormat[toFormat.length - 1];
let longYear = 0;
let shortYear = 0;
let year = '';
let month = 0;
let day = 0;
const dateArray = [];

for (let i = 0; i < fromFormat.length - 1; i++) {
if (fromFormat[i] === 'YYYY') {
longYear = date.split(fromSpliter)[i];
} else if (fromFormat[i] === 'YY') {
shortYear = date.split(fromSpliter)[i];
} else if (fromFormat[i] === 'MM') {
month = date.split(fromSpliter)[i];
} else {
day = date.split(fromSpliter)[i];
}
}

for (let i = 0; i < toFormat.length - 1; i++) {
if (toFormat[i] === 'YYYY' && longYear !== 0) {
dateArray.push(longYear);
} else if (toFormat[i] === 'YY' && shortYear !== 0) {
dateArray.push(shortYear);
} else if (toFormat[i] === 'YY' && longYear !== 0) {
year += longYear;
year = year.split('').splice(2).join('');
dateArray.push(Number(year));
} else if (toFormat[i] === 'YYYY' && shortYear !== 0) {
if (shortYear < 30) {
year += '20' + shortYear;
} else {
year += '19' + shortYear;
}
dateArray.push(Number(year));
} else if (toFormat[i] === 'MM') {
dateArray.push(month);
} else {
dateArray.push(day);
}
}

return dateArray.join(toSpliter);
}
module.exports = formatDate;

0 comments on commit bccde19

Please sign in to comment.