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 #2184

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

Choose a reason for hiding this comment

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

The split method uses fromFormat[3] as the delimiter, assuming the separator is always at this index. This might not be correct if the format array doesn't have a separator at index 3. Consider dynamically determining the separator based on the format string.

const result = [];

result[toFormat.indexOf('DD')] = dateParts[fromFormat.indexOf('DD')];
result[toFormat.indexOf('MM')] = dateParts[fromFormat.indexOf('MM')];

let year = fromFormat.includes('YYYY')
? dateParts[fromFormat.indexOf('YYYY')]
: dateParts[fromFormat.indexOf('YY')];

if (!toFormat.includes('YYYY')) {
year = year.length > 2 ? year.slice(2, 4) : year;
result[toFormat.indexOf('YY')] = year;
} else {
year = year.length === 2 ? (year < 30 ? `20${year}` : `19${year}`) : year;
result[toFormat.indexOf('YYYY')] = year;
}

return result.join(toFormat[3]);

Choose a reason for hiding this comment

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

The join method uses toFormat[3] as the separator, assuming the separator is always at this index. This might not be correct if the format array doesn't have a separator at index 3. Consider dynamically determining the separator based on the format string.

}

module.exports = formatDate;
Loading