티스토리 뷰

   간단하게 짤 수 있는 코드지만, 항상 필요할 때마다 타이핑하자니 귀찮아서 여기에 저장해놔야겠다.


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
32
33
34
35
function setPeriod(period){
    var startDate = getPastDate(period);
    var endDate = getRecentDate();
 
    $("#startDate").val(startDate);
    $("#endDate").val(endDate);
}
 
function getRecentDate(){
    var dt = new Date();
 
    var recentYear = dt.getFullYear();
    var recentMonth = dt.getMonth() + 1;
    var recentDay = dt.getDate();
 
    if(recentMonth < 10) recentMonth = "0" + recentMonth;
    if(recentDay < 10) recentDay = "0" + recentDay;
 
    return recentYear + "-" + recentMonth + "-" + recentDay;
}
 
function getPastDate(period){
    var dt = new Date();
 
    dt.setMonth((dt.getMonth() + 1) - period);
 
    var year = dt.getFullYear();
    var month = dt.getMonth();
    var day = dt.getDate();
 
    if(month < 10) month = "0" + month;
    if(day < 10) day = "0" + day;
 
    return year + "-" + month + "-" + day;
}


   getRecentDate() : 오늘 날짜를 반환한다. 오늘이 2018년 7월 20일이면 "2018-07-20"을 반환한다.


   getPastDate(period) : 3개월 전의 연월일을 얻고 싶다면, period를 3으로 넘긴다. 그럼 "2018-04-20"이 반환된다.


   setPeriod(period) : 위의 두 메서드를 사용하는 메서드다. "startDate", "endDate"라는 id를 갖고있는 text 타입의 input 태그에 값을 넣어주는 역할을 한다.



댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/04   »
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
글 보관함