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

Solution #2181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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 fromSeparator = fromFormat[3];
const toSeparator = toFormat[3];
const dateParts = date.split(fromSeparator);
const dateSet = {};

for (let i = 0; i < dateParts.length; i++) {
dateSet[fromFormat[i]] = dateParts[i];
}

const newFormat = [];

for (const key of toFormat) {
if (dateSet[key]) {
newFormat.push(dateSet[key]);
} else if (key === 'YY') {
newFormat.push(dateSet.YYYY.slice(2));
} else if (key === 'YYYY') {
if (dateSet.YY < 30) {
newFormat.push('20' + dateSet.YY);
} else {
newFormat.push('19' + dateSet.YY);

Choose a reason for hiding this comment

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

The logic for converting 'YY' to 'YYYY' is based on a fixed cutoff year of 30, which may not be suitable for all use cases. Consider making this logic more flexible or configurable, or clarify the assumptions in the documentation.

}
}

Choose a reason for hiding this comment

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

The function does not handle cases where the date format specified in fromFormat does not match the actual format of the date string. Consider adding validation to ensure the date string matches the fromFormat before processing.

}

return newFormat.join(toSeparator);
}

module.exports = formatDate;
Loading