본문 바로가기

Dev-/java script, jQuery, Ajax

ES6 날짜 추출,(배열 비구조화)

ES6에는 객체와 배열에서 비구조화(destructuring)할당이라는 선언법이 존재한다.


위 문법에 대한 설명 포스트는 아니고,,

날짜관련해서 조금 더 축약해서 쓸 수 있는 방법이 존재해 적어둔다.

const today = new Date(); / Tue May 21 2019 22:19:42 GMT+0900 (한국 표준시)
const formattedDate = today.toISOString().substring(0, 10); / "2019-05-21"
const [year, month, day] = formattedDate.split('-');
console.log([year, month, day]); / [ '2019', '05', '21' ]