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

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
80 changes: 79 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,85 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const [aFrom, bFrom, cFrom, separator] = fromFormat;
const fromFormatArray = [aFrom, bFrom, cFrom];
const [aTo, bTo, cTo, newSeparator] = toFormat;
const toFormatArray = [aTo, bTo, cTo];
const dateinNumbers = date.split(separator);

Choose a reason for hiding this comment

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

Ensure that the date string contains the expected number of segments when split by separator. If the date string does not match the expected format, this could lead to unexpected behavior or errors.


const dateInfo = getDateFromOld(fromFormatArray, dateinNumbers);

return setDateOrderNew(toFormatArray, dateInfo).join(newSeparator);

Choose a reason for hiding this comment

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

The setDateOrderNew function assumes that dateInfo contains all necessary date parts. Ensure that getDateFromOld correctly populates dateInfo with all required fields, or handle cases where some fields might be missing.

}

function getDateFromOld(fromArray, dateNumbers) {
const dateObj = {};

for (let i = 0; i < fromArray.length; i++) {
switch (fromArray[i]) {
case 'DD': {
dateObj.day = dateNumbers[i];
break;
}

case 'MM': {
dateObj.month = dateNumbers[i];
break;
}

case 'YYYY': {
dateObj.year = dateNumbers[i];
break;
}

case 'YY': {
if (dateNumbers[i] < 30) {
dateObj.year = `20${dateNumbers[i]}`;
} else {
dateObj.year = `19${dateNumbers[i]}`;
Comment on lines +43 to +46

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 for 'YY' format assumes that any year less than 30 belongs to the 2000s, and any year 30 or greater belongs to the 1900s. This is a common heuristic but may not be universally applicable. Consider making this logic configurable or document this assumption clearly.

}
break;
}

default: {
}
}
}

return dateObj;
}

function setDateOrderNew(toArray, dateNumbersObj) {
const dateInArray = [];
const objToOperate = { ...dateNumbersObj };

for (let i = 0; i < toArray.length; i++) {
switch (toArray[i]) {
case 'DD': {
dateInArray.push(objToOperate.day);
break;
}

case 'MM': {
dateInArray.push(objToOperate.month);
break;
}

case 'YYYY': {
dateInArray.push(objToOperate.year);
break;
}

case 'YY': {
dateInArray.push(objToOperate.year.slice(-2));
break;
}

default: {
}
}
}

return dateInArray;
}
module.exports = formatDate;
Loading