>  기사  >  웹 프론트엔드  >  JavaScript_Basic 지식 중 Date 객체의 일반적인 메소드 예

JavaScript_Basic 지식 중 Date 객체의 일반적인 메소드 예

WBOY
WBOY원래의
2016-05-16 15:35:241599검색

getFullYear()
연도를 얻으려면 getFullYear()를 사용하세요.
소스 코드:

</script>
<!DOCTYPE html>
<html>
<body>
&#8203;
<p id="demo">Click the button to display the full year of todays date.</p>
&#8203;
<button onclick="myFunction()">Try it</button>
&#8203;
<script>
function myFunction()
{
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.getFullYear();
}
</script>
&#8203;
</body>
</html>     

테스트 결과:

2015


getTime()
getTime()은 1970년 1월 1일 이후의 밀리초 수를 반환합니다.
소스 코드:

<!DOCTYPE html>
<html>
<body>
&#8203;
<p id="demo">Click the button to display the number of milliseconds since midnight, January 1, 1970.</p>
&#8203;
<button onclick="myFunction()">Try it</button>
&#8203;
<script>
function myFunction()
{
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.getTime();
}
</script>
&#8203;
</body>
</html> 


테스트 결과:

1445669203860

setFullYear()
setFullYear()를 사용하여 특정 날짜를 설정하는 방법입니다.
소스 코드:

<!DOCTYPE html>
<html>
<body>
&#8203;
<p id="demo">Click the button to display a date after changing the year, month, and day.</p>
&#8203;
<button onclick="myFunction()">Try it</button>
&#8203;
<script>
function myFunction()
{
var d = new Date();
d.setFullYear(2020,10,3);
var x = document.getElementById("demo");
x.innerHTML=d;
}
</script>
&#8203;
<p>Remember that JavaScript counts months from 0 to 11.
Month 10 is November.</p>
</body>
</html>     

테스트 결과:

Tue Nov 03 2020 14:47:46 GMT+0800 (中国标准时间)

toUTCString()
toUTCString()을 사용하여 오늘 날짜(UTC 기준)를 문자열로 변환하는 방법.

소스코드:

<!DOCTYPE html>
<html>
<body>
&#8203;
<p id="demo">Click the button to display the UTC date and time as a string.</p>
&#8203;
<button onclick="myFunction()">Try it</button>
&#8203;
<script>
function myFunction()
{
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.toUTCString();
}
</script>
&#8203;
</body>
</html> 

테스트 결과:

Sat, 24 Oct 2015 06:49:05 GMT

getDay()
getDay()와 배열을 사용하여 숫자뿐만 아니라 요일도 표시하는 방법입니다.
소스 코드:

<!DOCTYPE html>
<html>
<body>
&#8203;
<p id="demo">Click the button to display todays day of the week.</p>
&#8203;
<button onclick="myFunction()">Try it</button>
&#8203;
<script>
function myFunction()
{
var d = new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
&#8203;
var x = document.getElementById("demo");
x.innerHTML=weekday[d.getDay()];
}
</script>
&#8203;
</body>
</html>  

테스트 결과:

Saturday

시계 표시
웹페이지에 시계를 표시하는 방법.
소스 코드:

<!DOCTYPE html>
<html>
<head>
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
&#8203;
function checkTime(i)
{
if (i<10)
 {
 i="0" + i;
 }
return i;
}
</script>
</head>
&#8203;
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>    


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.