-
javascript 날짜처리 함수 (증감)언어의 온도/javascript&jquery 2023. 3. 22. 10:24
* STEP1. 날짜처리 함수 선언을 해보자~
//일별 증감 Date.prototype.addDays = function (days) { let date = this; return new Date(date.setDate(date.getDate() + days)); }; //월별 증감 Date.prototype.addMonth = function (month) { let date = this; return new Date(date.setMonth(date.getMonth() + month)); }; //년별 증감 Date.prototype.addYear = function (year) { let date = this; return new Date(date.setFullYear(date.getFullYear() + year)); };
* STEP2. 날짜를 가지고 놀아보자~
//날짜 세팅 초기화 ( 테스트시 셋중 한개만 선언하자 ) const date = new Date().addDays(-7); //7일전 const date = new Date().addMonth(-1); //1달전 const date = new Date().addYear(-1); //1년전 //YYYY-MM-DD 처리 const year = date.getFullYear(); const month = ('0' + (date.getMonth() + 1)).slice(-2); const day = ('0' + date.getDate()).slice(-2); const dateStr = year+'-'+month+'-'+day; console.log(dateStr);
▶ 결과
참~쉽죠잉~
'언어의 온도 > javascript&jquery' 카테고리의 다른 글
modal hidden 이벤트처리 (0) 2023.03.22 javascript localStorage (0) 2023.03.22 sheetjs json 엑셀다운로드 (0) 2023.03.22 json 원하는 필드만 가져오기 (0) 2023.03.22 javascript 마스킹 처리 (0) 2023.03.15