ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 음력달력 연습 2
    Java Script 2019. 2. 11. 17:24
    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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    <!DOCTYPE html>
    <html lang="ko">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
        <!-- 숫자를 포맷하고 조작하기위한 자바 스크립트 라이브러리 -->
        <script src="http://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
        <!-- 날짜를 포맷하고 조작하기위한 자바 스크립트 라이브러리 -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ko.js"></script>
        <!-- Jquery -->
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script>
          //===========================================================================
          // 윤년을 판단하는 함수
          function isLeapYear(year) {
            return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
          }
          // 월의 마지막 날짜를 리턴하는 함수
          function getLastDayOfMonth(year, month) {
            var monthDay = [312831303130313130313031];
            monthDay[1= isLeapYear(year) ? 29 : 28;
            return monthDay[month - 1];
          }
          // 1년 1월 1일부터 지정날짜까지의 총일수
          function getTotalDay(year, month, day) {
            var totalDay =
              (year - 1* 365 +
              Math.floor((year - 1/ 4-
              Math.floor((year - 1/ 100+
              Math.floor((year - 1/ 400);
            for (var i = 1; i < month; i++) totalDay += getLastDayOfMonth(year, i);
            totalDay += day;
            return totalDay;
          }
          // 요일을 숫자로
          function getDayOfWeekNum(year, month, day) {
            return getTotalDay(year, month, day) % 7;
          }
          // 요일을 문자로
          function getDayOfWeekStr(year, month, day) {
            return "일월화수목금토".charAt(getDayOfWeekNum(year, month, day));
          }
          // 생활연령 계산하기
          // 알고자하는 년,월,일
          function getAgeCompute(year, month, date) {
            // 생활연령 계산
            var nd = new Date(); // 현재 년월일
            var id = new Date(year, month - 1, date); // 알고자하는 년월일을 이용하여 Date객체 생성
     
            var rd = (nd.getTime() - id.getTime()) / 1000 / 60 / 60 / 24;
            var ryear = nd.getFullYear() - id.getFullYear();
            var rmonth = nd.getMonth() - id.getMonth();
            var rdate = nd.getDate() - id.getDate();
     
            if (rdate < 0) {
              rmonth--;
              rdate += 30;
            }
            if (rmonth < 0) {
              ryear--;
              rmonth += 12;
            }
            var rmonthMix = ryear * 12 + rmonth;
     
            var obj = { year: year, month: month, date: date };
            obj.rYear = ryear; // ?세
            obj.rMonth = rmonth; // ?개월
            obj.rDate = rdate; // ?일
            // 살아온날
            obj.rMonthMix = rmonthMix; // 몇개월 살았는가?
            obj.totalday =
              getTotalDay(nd.getFullYear(), nd.getMonth() + 1, nd.getDate()) -
              getTotalDay(year, month, date) +
              1// 몇일 살았는가?
            obj.weekday =
              getDayOfWeekStr(id.getFullYear(), id.getMonth() + 1, id.getDate()) +
              "(" +
              getDayOfWeekNum(id.getFullYear(), id.getMonth() + 1, id.getDate()) +
              ")"// 요일
            // 나이 계산
            var agek = nd.getFullYear() - id.getFullYear() + 1;
            var agem = nd.getFullYear() - id.getFullYear() - 1;
            if (
              (nd.getMonth() >= id.getMonth() && nd.getDate() >= id.getDate()) ||
              nd.getMonth() > id.getMonth()
            ) {
              agem++;
            }
     
            obj.ageK = agek; // 우리 나이
            obj.ageM = agem; // 만나이
            return obj;
            //  년,월,일,?년,?개월,?일살았나,총개월수(rMonthMix),살아온일수(totalDay),요일(weekday),우리나이(ageK),만나이(ageM)
            // {"year":2019,"month":2,"date":7,"rYear":0,"rMonth":0,"rDate":0,"rMonthMix":0,"totalday":1,"weekday":"목(4)","ageK":1,"ageM":0}
          }
        </script>
      </head>
      <body>
        <script>
          document.writeln(JSON.stringify(getAgeCompute(201927)) + "<br>");
          document.writeln(getTotalDay(201928+ "<br>");
          document.writeln(getDayOfWeekNum(201928+ "<br>");
          document.writeln(getDayOfWeekStr(201928+ "<br>");
        </script>
      </body>
    </html>
     
    cs


    'Java Script' 카테고리의 다른 글

    Json  (0) 2019.05.29
    isNaN  (0) 2019.05.14
    음력달력 연습 3  (0) 2019.02.11
    음력달력 연습 1  (0) 2019.02.11
    음력달력 js  (1) 2019.02.11
Designed by Tistory.