-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Solution #2187
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
const dateInfo = getDateFromOld(fromFormatArray, dateinNumbers); | ||
|
||
return setDateOrderNew(toFormatArray, dateInfo).join(newSeparator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
There was a problem hiding this comment.
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 byseparator
. If thedate
string does not match the expected format, this could lead to unexpected behavior or errors.