배열안의 문자열로 제공되는 날짜가 입력됨
표준 날짜 형식으로 출력
날짜 변환하는 함수를 작성
function preprocessDate(a) {
let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
let year, month, day, final = ''
let f = []
for (let i = 0; i < a.length; i++) {
//총 길이가 13일때
if(a[i].length === 13){
year = a[i].slice(9,13) + '-'
day = a[i].slice(0,2)
month = (months.indexOf(a[i].slice(5, 8)) + 1) + '-'
} else {
//총 길이가 12일떄
year = a[i].slice(8,12) + '-'
month = '0' + (months.indexOf(a[i].slice(4, 7)) + 1) + '-'
day = '0' + a[i].slice(0,1)
}
// month가 한자리이고, Dec 가 13일때
if(month.length === 2){
month = '0' + month
} else if (month === 13){
month = '12-'
}
final = year + month + day
f.push(final)
}
return f
}
console.log(preprocessDate([
'20th Oct 2052',
'6th Jun 1933',
'26th May 1960',
'20th Sep 1958',
'16th Mar 2068',
'25th May 1912',
'16th Dec 2018',
'26th Dec 2061',
'4th Nov 2030',
'28th Jul 1963']))
[ '2052-10-20', '1933-06-06', '1960-05-26', '1958-09-20', '2068-03-16', '1912-05-25', '2018-12-16', '2061-12-02', '2030-11-04', '1963-07-28' ]