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

Test #2174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Test #2174

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
41 changes: 39 additions & 2 deletions src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,49 @@
/**
* @param {string} date
* @param {string[]} fromFormat
* @param {string[]} toFormat
* @param {string[]} toFormat.
*
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const splitSeparator = fromFormat[fromFormat.length - 1];
const splitDate = date.split(splitSeparator);

const joinSeparator = toFormat[toFormat.length - 1];

// variables of index date fromFormate

Choose a reason for hiding this comment

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

The comment here has a typo: 'fromFormate' should be 'fromFormat'.

const fromYYYY = fromFormat.includes('YY')
? fromFormat.indexOf('YY')
: fromFormat.indexOf('YYYY');
const fromMM = fromFormat.indexOf('MM');
const fromDD = fromFormat.indexOf('DD');

// variables of index date toFormate

Choose a reason for hiding this comment

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

The comment here has a typo: 'toFormate' should be 'toFormat'.

const toYYYY = toFormat.includes('YY')
? toFormat.indexOf('YY')
: toFormat.indexOf('YYYY');
const toMM = toFormat.indexOf('MM');
const toDD = toFormat.indexOf('DD');

if (fromFormat[fromYYYY].length > toFormat[toYYYY].length) {
splitDate[fromYYYY] = splitDate[fromYYYY].slice(2);

Choose a reason for hiding this comment

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

This line slices the year to two digits if the 'fromFormat' year is longer than the 'toFormat' year. Ensure that this logic aligns with the expected behavior, especially if the formats might not always be 'YY' or 'YYYY'.

}

if (fromFormat[fromYYYY].length < toFormat[toYYYY].length) {
if (+splitDate[fromYYYY] < 30) {
splitDate[fromYYYY] = '20' + splitDate[fromYYYY];
} else {
splitDate[fromYYYY] = '19' + splitDate[fromYYYY];
Comment on lines +35 to +38

Choose a reason for hiding this comment

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

The logic for determining the century ('20' or '19') based on the year being less than 30 might not be universally applicable. Consider whether this assumption holds true for all expected input dates.

}
}

const result = [];

result[toYYYY] = splitDate[fromYYYY];
result[toMM] = splitDate[fromMM];
result[toDD] = splitDate[fromDD];

return result.join(joinSeparator);
}

module.exports = formatDate;
Loading