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

Completed the task #2169

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
27 changes: 26 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,32 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const separator = fromFormat[3];

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 separator is at index 3 of the fromFormat array. This assumption might not hold true if the format is different, such as 'YYYY-MM-DD'. Consider revising the logic to dynamically determine the separator based on the actual date string or format pattern.

const parts = date.split(separator);

const dateObj = {};

for (let i = 0; i < 3; i++) {
dateObj[fromFormat[i]] = parts[i];
}

if (fromFormat.includes('YYYY') && toFormat.includes('YY')) {
dateObj['YY'] = dateObj['YYYY'].slice(-2);
}

if (fromFormat.includes('YY') && toFormat.includes('YYYY')) {
const year = parseInt(dateObj['YY'], 10);

dateObj['YYYY'] = year < 30 ? `20${dateObj['YY']}` : `19${dateObj['YY']}`;
}

const newSeparator = toFormat[3];

Choose a reason for hiding this comment

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

Similar to the issue with fromFormat, the code assumes the separator is at index 3 of the toFormat array. This might not be correct for all formats. Ensure that the separator is correctly identified or passed separately.

const newDate = toFormat
.slice(0, 3)
.map((part) => dateObj[part])
.join(newSeparator);

return newDate;
}

module.exports = formatDate;
Loading