Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 개념
- Redux
- 종류
- 비교
- 코딩 기초 트레이닝
- 장단점
- 과제
- input
- 부트캠프
- backjun
- 차이점
- 안드로이드
- React
- 코틀린
- javascript
- 의미
- 스파르타코딩클럽
- 자바스크립트
- programmers
- 리액트
- 문법
- 코딩기초트레이닝
- 장점
- 단점
- 웹개발종합반
- Android
- 특징
- Promise
- 프로그래머스
- 항해99
Archives
COCO World
[JavaScript/자바스크립트] new Date() 날짜 계산하기 (어제, 내일, 한달 전/후, 일년 전, 날짜 더하기/빼기) 본문
Language/JavaScript
[JavaScript/자바스크립트] new Date() 날짜 계산하기 (어제, 내일, 한달 전/후, 일년 전, 날짜 더하기/빼기)
코코월드주인장 2023. 10. 17. 11:30🎃 오늘 날짜
const now = new Date();
# 출력
console.log("오늘 날짜 : ")
console.log(now)
출력
🎃 어제, 내일
const now = new Date();
# 어제
const yesterday = new Date(new Date().setDate(new Date().getDate() - 1));
# 내일
const tomorrow = new Date(new Date().setDate(new Date().getDate() + 1));
# 출력
console.log("어제 날짜 : ")
console.log(yesterday)
console.log("내일 날짜 : ")
console.log(tomorrow)
출력
🎃 며칠 전/후
# 며칠 전(ex: 3일 전)
const threeDaysAgo = new Date(new Date().setDate(new Date().getDate() - 3));
# 며칠 전(ex: 3일 후)
const threeDaysAfter = new Date(new Date().setDate(new Date().getDate() + 3));
# 출력
console.log("3일 전 날짜 : ")
console.log(threeDaysAgo)
console.log("3일 후 날짜 : ")
console.log(threeDaysAfter)
출력
🎃 한달 전 / 한달 후
# 한달 전
const oneMonthAgo = new Date(new Date().setMonth(new Date().getMonth() - 1));
# 한달 후
const oneMonthAfter = new Date(new Date().setMonth(new Date().getMonth() + 1));
# 출력
console.log("한달 전 날짜 : ")
console.log(oneMonthAgo)
console.log("한달 후 날짜 : ")
console.log(oneMonthAfter)
출력
🎃 몇달 전 / 몇달 후
# 4개월 전
const fourMonthAgo = new Date(new Date().setMonth(new Date().getMonth() - 4));
# 4개월 후
const fourMonthAfter = new Date(new Date().setMonth(new Date().getMonth() + 4));
# 출력
console.log("4개월 전 날짜 : ")
console.log(fourMonthAgo)
console.log("4개월 후 날짜 : ")
console.log(fourMonthAfter)
출력
🎃 일년 전 / 일년 후
# 1년 전
const oneYearAgo = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
# 1년 후
const oneYearAfter = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
# 출력
console.log("1년 전 날짜 : ")
console.log(oneYearAgo)
console.log("1년 후 날짜 : ")
console.log(oneYearAfter)
출력
🎃 몇년 전 / 몇년 후
# 13년 전
const thirteenYearAgo = new Date(new Date().setFullYear(new Date().getFullYear() - 13));
# 13년 후
const thirteenYearAfter = new Date(new Date().setFullYear(new Date().getFullYear() + 13));
# 출력
console.log("13년 전 날짜 : ")
console.log(thirteenYearAgo)
console.log("13년 후 날짜 : ")
console.log(thirteenYearAfter)
출력
간만에 블로그 애정도를 끌어올리기 위해 검색어필터에 기간 적용을 하다 게시글까지 업데이트 해보았다.
오후 1시 22분, 밖에 가을햇살 너무 좋네

'Language > JavaScript' 카테고리의 다른 글
[JavaScript/자바스크립트] 문자열의 양 옆 사이드 공백 제거하기 - trim(), replace() (0) | 2023.08.11 |
---|---|
[Javascript/자바스크립트] Day5 - async와 await (1) | 2023.01.26 |
[Javascript/자바스크립트] Day5 - 프라미스(Promise)와 체이닝(chaining), 프라미스 API (0) | 2023.01.26 |
[Javascript/자바스크립트] Day4 - this와 콜백, 전역객체, 함수, 메서드, 추상화 (0) | 2023.01.26 |
[Javascript/자바스크립트] 자료형 - 배열 (0) | 2023.01.25 |