Knowledge Map

angularjs 윤년 처리 본문

WEB/AngularJS

angularjs 윤년 처리

2016. 4. 1. 17:30

angularjs에서 윤년 처리할수 있는 콤보 박스를 만들어 보았다.

사실.. 그냥 datepicker을 하고 싶었으나 콤보 박스 하라고 하니 어쩌겠나. 해야지..


일단 더럽게 못짠 코드라서 그냥 정말 헤매는 사람만 참고삼아 보길 바란다.

angularJS버전은 1.4.2이다.




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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//변수 선언을 해준다. 
//monthData를 통해서 html화면에 ng-repeat로 뿌려주려고 한다.
//dayData를 통해서 각 월별로 날수들을 읽어들일수 있게 한다.
//dayMonthData는 dayData에서 해당하는 월의 날수를 1부터 저장하게 한다.
//year, month, day는 각각 선택한 날짜 값들에 대해서 저장할수 있도록 한다.
$scope.abc= {
    birth:{
        field:"birth", value:null, status:0,notice:null,year:null,month:null,day:null,
        yearData:[],
        monthData:[123456789101112],
        dayData:[312831303130313130313031],
        dayMonthData:[]
    },
};
 
//for구문을 돌리는 이유는 yearData에 1900년부터 2020년까지 출력하게끔 하기위해서다.
for(var i = 0; i < 120; i++){
    $scope.abc.birth.yearData[i]=i+1900;
}
 
//leapYear은 윤년계산을 위한 것이다.
//4의 배수이면서 100의 배수가 아닌 해가 윤년이며
//4의 배수면서 100의 배수이면서 400의 배수이면 또한 윤년이다.
 
$scope.leapYear = function(){
 
 
//1번방식
    if($scope.abc.birth.year%4 == 0 && $scope.abc.birth.year%100 != 0){
        $scope.abc.birth.dayData[1= 29;
    }else{
        if($scope.abc.birth.year%400 == 0) {
            $scope.abc.birth.dayData[1= 29;
         }else{
             $scope.abc.birth.dayData[1= 28;
        }
    }
//끝 1번방식
 
//2번방식 (이거 정리하다가 생각남)
 
    if($scope.abc.birth.year%400 == 0 || ($scope.abc.birth.year%100 != 0 && $scope.abc.birth.year%4 == 0)){
        $scope.abc.birth.dayData[1= 29;
    }else{
        $scope.abc.birth.dayData[1= 28;
    }
 
//끝 2번방식
//보통 왼쪽 --> 오른쪽으로 이동한다. ||는 or 이라서 true가 뜨면 바로 넘어간다고 알고 있다.
//400의 배수는 100과 4의 배수를 포함하므로 저렇게 비교하면 바로 넘어가지며
//400의 배수가 아닌 값들 중에 100의 배수를 제외한 4의 배수를 구해야 하기 때문에 저렇게 쓰면된다.
//굳이 else를 쓰는 이유는 29로 바뀌었다가 다시 28로 바뀌어야 할 경우도 있기 때문이다. (잘못 클릭했다거나 등등)
 
     $scope.abc.birth.dayMonthData = [' '];
 
//초기화다. 이 방식은 배열에 31, 30, 29, 28까지 넣는 방식 이기 때문에 31까지 넣었다가 다시 28까지 넣으면 29,30,31값이 그대로 남아있게된다.
//따라서 그값들을 지워주어야 한다.
 
     for(var i=0; i<$scope.abc.birth.dayData[$scope.abc.birth.month-1]; i++){
         $scope.abc.birth.dayMonthData[i] = i+1
 
//월에 대한 날수를 1부터 죽 입력해 넣는다.
    }
};
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- ng-repeat를 사용한 html코드. 단순하다. -->
<!-- leapYear()에 ng-change를 걸어서 변할때마다 다시 반영되게 만들었다. -->
 
<div class="list-group-item birth" >
    <select class="form-control" ng-model="abc.birth.year" ng-change="leapYear()">
        <option ng-repeat="year in abc.birth.yearData">{{year}}</option>
    </select>
    <select class="form-control" ng-model="abc.birth.month" ng-change="leapYear()">
        <option ng-repeat="month in abc.birth.monthData">{{month}}</option>
    </select>
    <select class="form-control" ng-model="abc.birth.day">
        <option ng-repeat="day in abc.birth.dayMonthData">{{day}}</option>
    </select>
</div>
cs




'WEB > AngularJS' 카테고리의 다른 글

간단한 ng-if  (0) 2016.04.14
[$injector:unpr] Unknown provider 에러코드  (0) 2016.04.06
angularJS에서 버튼 생성  (0) 2016.03.31
placeholder 에 angularjs translate적용하기  (0) 2016.03.31
URL막기 / 단체 체크, 체그 해제  (0) 2016.03.29
Comments