-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
add solution #2191
base: master
Are you sure you want to change the base?
add solution #2191
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,70 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const [, , , slider] = fromFormat; | ||
const [, , , glue] = toFormat; | ||
const [year, month, day] = normalizeDate( | ||
date.split(slider), | ||
fromFormat, | ||
toFormat, | ||
); | ||
|
||
const newDate = []; | ||
|
||
for (let i = 0; i < toFormat.length - 1; i++) { | ||
if (toFormat[i].includes('YY') || toFormat[i].includes('YYYY')) { | ||
newDate.push(year); | ||
continue; | ||
} | ||
|
||
if (toFormat[i].includes('MM')) { | ||
newDate.push(month); | ||
continue; | ||
} | ||
|
||
if (toFormat[i].includes('DD')) { | ||
newDate.push(day); | ||
continue; | ||
} | ||
} | ||
|
||
return newDate.join(glue); | ||
} | ||
|
||
function normalizeDate(dateMass, formatMass, toFormat) { | ||
const normalDate = []; | ||
|
||
for (let i = 0; i < dateMass.length; i++) { | ||
if (formatMass[i].includes('YY') || formatMass[i].includes('YYYY')) { | ||
normalDate[0] = dateMass[i]; | ||
continue; | ||
} | ||
|
||
if (formatMass[i].includes('MM')) { | ||
normalDate[1] = dateMass[i]; | ||
continue; | ||
} | ||
|
||
if (formatMass[i].includes('DD')) { | ||
normalDate[2] = dateMass[i]; | ||
continue; | ||
} | ||
} | ||
|
||
if (normalDate[0].length <= 2 && toFormat.includes('YYYY')) { | ||
if (+normalDate[0] >= 30) { | ||
normalDate[0] = +normalDate[0] + 1900; | ||
} else { | ||
normalDate[0] = +normalDate[0] + 2000; | ||
} | ||
} else if (normalDate[0].length > 2 && toFormat.includes('YY')) { | ||
if (+normalDate[0] < 2000) { | ||
normalDate[0] = +normalDate[0] - 1900; | ||
} else { | ||
normalDate[0] = +normalDate[0] - 2000; | ||
} | ||
} | ||
Comment on lines
+61
to
+73
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 converting two-digit years to four-digit years and vice versa may not work correctly for all cases. For example, if the input year is '29', it will be converted to '2029', which might not be the intended behavior. Consider adding more robust logic to handle different century assumptions or provide a way for the user to specify the century. |
||
|
||
return normalDate; | ||
} | ||
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.
The code assumes that the fourth element in
fromFormat
andtoFormat
arrays is the delimiter. This assumption might not always hold true, especially if the format arrays do not have exactly four elements. Consider adding a check or a more flexible way to determine the delimiter.