사용자 도구

사이트 도구


wiki:javascript:javascript_note:js_date_get_methods

JavaScript Get Date Methods

  • description : JavaScript Get Date Methods
  • author : 오션
  • email : shlim@repia.com
  • lastupdate : 2021-04-13

Ref

JavaScript Get Date Methods

다음의 메소드들은 날짜 오브젝트에서 정보를 가져오기 위해 사용할 수 있습니다.

Method Description
getFullYear() 연도를 4자리 숫자로 가져옵니다.
getMonth() 월을 숫자(0~11)로 가져옵니다.
getDate() 날짜를 숫자(0~31)로 가져옵니다.
getHours() 시간(0~23)을 가져옵니다.
getMinutes() 분(0~59)을 가져옵니다.
getSeconds() 초(0~59)를 가져옵니다.
getMilliseconds() 밀리 초(0~999)를 가져옵니다.
getTime() 1970년 1월 1일 이후의 밀리 초 시간을 가져옵니다.
getDay() 요일을 숫자(0~6)로 가져옵니다.
Date.now() 시간을 가져옵니다. ECMAScript 5


The getTime() Method

getTime() 메서드는 1970 년 1 월 1 일 이후의 밀리 초 수를 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getTime();  // 1618280425527

The getFullYear() Method

getFullYear() 메서드는 날짜의 연도를 4 자리 숫자로 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();   // 2021

The getMonth() Method

getMonth() 메서드는 날짜의 월을 숫자 (0-11)로 반환합니다.

<!DOCTYPE html>
<html>
<body>
    <h2>JavaScript getMonth()</h2>
    <p>The getMonth() Method returns the month of a date as a number from 0 to 11.</p>
    <p>To get the correct month, you must add 1:</p>
    <p id="demo"></p>
    <script>
        var d = new Date();
        document.getElementById("demo").innerHTML = d.getMonth() + 1;   // 4
    </script>
</body>
</html>


JavaScript에서 첫 번째 달 (1 월)은 월 번호 0이므로 12 월은 월 번호 11을 반환합니다.

이름 배열을 사용하고, getMonth()를 사용하여 월을 이름으로 반환할 수 있습니다.

<!DOCTYPE html>
<html>
<body>
    <h2>JavaScript getMonth()</h2>
    <p>The getMonth() method returns the month as a number:</p>
    <p>You can use an array to display the name of the month:</p>
    <p id="demo"></p>
    <script>
        var d = new Date();
        var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        document.getElementById("demo").innerHTML = months[d.getMonth()];   // Month will be displayed.
    </script>
</body>
</html>
</html>

The getDate() Method

getDate() 메서드는 날짜를 숫자 (1-31)로 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getDate();  // 오늘 날짜 표시

The getHours() Method

getHours() 메서드는 날짜의 시간(시)을 숫자 (0-23)로 반환합니다.

var d = new Date();
document.getElementById("demo).innerHTML = d.getHours();  // 현재의 시 표시

The getMinutes() Method

getMinutes() 메서드는 날짜의 시간(분)을 숫자 (0~59)로 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getMinutes();  

The getSeconds() Method

getSeconds() 메서드는 날짜의 시간(초)을 숫자 (0~59)로 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getSeconds();

The getMilliseconds() Method

getMilliseconds() 메서드는 날짜의 밀리 초를 숫자 (0~999)로 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getMilliseconds();

The getDay() Method

getDay() 메서드는 날짜의 평일을 숫자 (0~6)로 반환합니다.

var d = new Date();
document.getElementById("demo").innerHTML = d.getDay();


세계의 일부 국가에서 일주일의 첫 번째 요일을 “월요일”로 간주하더라도,
JavaScript에서 일주일의 첫 번째 요일(0)은 “일요일”을 의미합니다.

이름 배열을 사용하고 getDay()를 사용하여 평일(weekday)을 이름으로 반환할 수 있습니다.

<!DOCTYPE html>
<html lang="en">
<body>
    <h2>JavaScript getDay()</h2>
    <p>The getDay() method returns the weekday as a number:</p>
    <p id="demo"></p>
    <script>
        var d = new Date();
        var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        document.getElementById("demo").innerHTML = days[d.getDay()];
    </script>
</body>
</html>

UTC Date Methods

UTC 날짜 방법은 UTC 날짜 (Universal Time Zone 날짜) 작업에 사용됩니다.

Method Description
getUTCDate() getDate()와 동일하지만 UTC날짜를 반환합니다.
getUTCDay() getDay()와 동일하지만 UTC 날짜를 반환합니다.
getUTCFullYear() getFullYear()와 동일하지만 UTC 년도를 반환합니다.
getUTCHours() getHours()와 동일하지만 UTC 시간을 반환합니다.
getUTCMilliseconds() getMilliseconds()와 동일하지만 UTC 밀리 초를 반환합니다.
getUTCMinutes() getMinutes()와 동일하지만 UTC 분을 반환합니다.
getUTCMonth() getMonth()와 동일하지만 UTC 월을 반환합니다.
getUTCSeconds() getSeconds()와 동일하지만 UTC 초를 반환합니다.



/var/services/web/dokuwiki/data/pages/wiki/javascript/javascript_note/js_date_get_methods.txt · 마지막으로 수정됨: 2023/01/13 18:44 (바깥 편집)