-
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 #2159
base: master
Are you sure you want to change the base?
Solution #2159
Changes from 2 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,69 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const separator = fromFormat[3]; | ||
const dateArray = date.split(separator); | ||
let day = ''; | ||
let month = ''; | ||
let year = ''; | ||
|
||
if (fromFormat[0] === 'YYYY' || fromFormat[0] === 'YY') { | ||
year = dateArray[0]; | ||
month = dateArray[1]; | ||
day = dateArray[2]; | ||
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. 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. ПОдскажи как это сделать? |
||
} else if (fromFormat[1] === 'YYYY') { | ||
year = dateArray[1]; | ||
month = dateArray[0]; | ||
day = dateArray[2]; | ||
} else { | ||
year = dateArray[2]; | ||
month = dateArray[1]; | ||
day = dateArray[0]; | ||
} | ||
|
||
if (toFormat[0] === 'YYYY' && year.length === 4) { | ||
return year + toFormat[3] + month + toFormat[3] + day; | ||
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 code assumes that the separator is always at index 3 of the
Comment on lines
+25
to
+26
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 code assumes the separator is at index 3 of the |
||
} | ||
|
||
if (toFormat[0] === 'YY' && year.length === 4) { | ||
return year.slice(2) + toFormat[3] + month + toFormat[3] + day; | ||
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. Similar to the previous comment, the assumption about the separator being at index 3 of the
Comment on lines
+29
to
+30
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 separator is assumed to be at index 3 of the |
||
} | ||
|
||
if (toFormat[0] === 'YY' && year.length === 2) { | ||
return year + toFormat[3] + month + toFormat[3] + day; | ||
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. Again, the assumption about the separator at index 3 of the
Comment on lines
+33
to
+34
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 separator is assumed to be at index 3 of the |
||
} | ||
|
||
if (toFormat[0] === 'YYYY' && year.length === 2) { | ||
if (+year >= 30) { | ||
year = '19' + year; | ||
} else { | ||
year = '20' + year; | ||
} | ||
|
||
return year + toFormat[3] + month + toFormat[3] + day; | ||
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 separator is assumed to be at index 3 of the
Comment on lines
+37
to
+44
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 separator is assumed to be at index 3 of the |
||
} | ||
|
||
if (toFormat[2] === 'YYYY' && year.length === 4) { | ||
return day + toFormat[3] + month + toFormat[3] + year; | ||
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 code assumes the separator is at index 3 of the
Comment on lines
+47
to
+48
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 separator is assumed to be at index 3 of the |
||
} | ||
|
||
if (toFormat[2] === 'YY' && year.length === 4) { | ||
return day + toFormat[3] + month + toFormat[3] + year.slice(2); | ||
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 assumption about the separator being at index 3 of the
Comment on lines
+51
to
+52
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 separator is assumed to be at index 3 of the |
||
} | ||
|
||
if (toFormat[2] === 'YY' && year.length === 2) { | ||
return day + toFormat[3] + month + toFormat[3] + year; | ||
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 separator is assumed to be at index 3 of the
Comment on lines
+55
to
+56
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 separator is assumed to be at index 3 of the |
||
} | ||
|
||
if (toFormat[2] === 'YYYY' && year.length === 2) { | ||
if (+year >= 30) { | ||
year = '19' + year; | ||
} else { | ||
year = '20' + year; | ||
} | ||
|
||
return day + toFormat[3] + month + toFormat[3] + year; | ||
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 code assumes the separator is at index 3 of the
Comment on lines
+59
to
+66
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 separator is assumed to be at index 3 of the |
||
} | ||
} | ||
|
||
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 separator is assumed to be at index 3 of the
fromFormat
array, which may not be correct for all date formats. Consider dynamically determining the separator based on the actual format provided.