Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add solution #2191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
65 changes: 64 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,70 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const [, , , slider] = fromFormat;
const [, , , glue] = toFormat;
Comment on lines +11 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that the fourth element in fromFormat and toFormat arrays is the delimiter. This assumption might not always hold true, especially if the format arrays do not have exactly four elements. Consider adding a check or a more flexible way to determine the delimiter.

const [year, month, day] = normalizeDate(
date.split(slider),
fromFormat,
toFormat,
);

const newDate = [];

for (let i = 0; i < toFormat.length - 1; i++) {
if (toFormat[i].includes('YY') || toFormat[i].includes('YYYY')) {
newDate.push(year);
continue;
}

if (toFormat[i].includes('MM')) {
newDate.push(month);
continue;
}

if (toFormat[i].includes('DD')) {
newDate.push(day);
continue;
}
}

return newDate.join(glue);
}

function normalizeDate(dateMass, formatMass, toFormat) {
const normalDate = [];

for (let i = 0; i < dateMass.length; i++) {
if (formatMass[i].includes('YY') || formatMass[i].includes('YYYY')) {
normalDate[0] = dateMass[i];
continue;
}

if (formatMass[i].includes('MM')) {
normalDate[1] = dateMass[i];
continue;
}

if (formatMass[i].includes('DD')) {
normalDate[2] = dateMass[i];
continue;
}
}

if (normalDate[0].length <= 2 && toFormat.includes('YYYY')) {
if (+normalDate[0] >= 30) {
normalDate[0] = +normalDate[0] + 1900;
} else {
normalDate[0] = +normalDate[0] + 2000;
}
} else if (normalDate[0].length > 2 && toFormat.includes('YY')) {
if (+normalDate[0] < 2000) {
normalDate[0] = +normalDate[0] - 1900;
} else {
normalDate[0] = +normalDate[0] - 2000;
}
}
Comment on lines +61 to +73

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for converting two-digit years to four-digit years and vice versa may not work correctly for all cases. For example, if the input year is '29', it will be converted to '2029', which might not be the intended behavior. Consider adding more robust logic to handle different century assumptions or provide a way for the user to specify the century.


return normalDate;
}
module.exports = formatDate;
Loading