IT 공부/JavaScript

Date 객체

toraa 2022. 6. 8. 17:37

Date객체
; 사용자 시스템의 날짜와 시간을 관리해주는 객체

. 객체생성 : 객체변수 = new Date();

. 메소드(method)  객체를 생성하지 않으면 만들 수 없음 (함수와 차이점)
  ~ getSetMethod ~
getFullYear() : 2022 과 같이 년도를 4자리로 반환해줌
getMonth() : 0 1월, 11 12월 
getDate()  : 한달을 31일 표기식으로 날짜반환
getDay()   : 요일 반환(0: 일요일 ~~ 6: 토요일)
getHour()  : 0시 ~ 23시
getMinutes(): 분
getSeconds(): 초
getTime()  : 1970-1-1 00:00:00 ~~~ 현재까지 경과한 시간을 1/1000초 단위로 표시
     ~
setFullYear(): 년도설정
setMonth()
setDate()
setHours()
setMinutes()
     ~

class Date{

String Year ;

String getFullYear(){
return 년도;
}


const current = new Date(); 시스템의 현재 날짜시간
const end = new Date(2022,9,16); 객체를 특정 날짜로 지정

let leftdate = end.getTime() - current.getTime();

 <html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body align = "center";>
<script type="text/javascript">
document.write("<img src=https://i1.sndcdn.com/avatars-000751960345-ath81o-t500x500.jpg width='300px' height='100%' >");

const current = new Date(); 
const end = new Date(2022,8,16);

document.write("<p><strong><h2>오늘은 ");
document.write(+ current.getFullYear() + "년 ".fontcolor("#DB7093"));
document.write(+ (current.getMonth()+1) + "월 ".fontcolor("#9370DB"));
document.write(+current.getDate()+"일 ".fontcolor("#9400D3"));
document.write("(=");

switch (current.getDay()){
case 0 : document.write("일요일".fontcolor("#FA8072"));break;
case 1 : document.write("월요일".fontcolor("#FA8072"));break;
case 2 : document.write("화요일".fontcolor("#FA8072"));break;
case 3 : document.write("수요일".fontcolor("#FA8072"));break;
case 4 : document.write("목요일".fontcolor("#FA8072"));break;
case 5 : document.write("금요일".fontcolor("#FA8072"));break;
case 6 : document.write("토요일".fontcolor("#FA8072"));break;
}
document.write(")");

let leftdate = end.getTime() - current.getTime();
 종료날짜: "end.getTime()
시작날짜: "current.getTime()

let d = leftdate/(24*60*60*1000);
document.write("<br>남은 수업 시간은 ");
document.write(+Math.floor(d)+"일".fontcolor("#F08080"));

document.write("</h2></p></strong>");

</script>


<body>
<h3>방문 시간이 짝수 초이면 violet, 홀수이면 lightskyblue</h3>
<hr>
<script type="text/javascript">

const current = new Date();

if(current.getSeconds() %2 == 0){
document.body.style.backgroundColor="violet";
}
else{
document.body.style.backgroundColor="lightskyblue";
}

document.write("<h4> 현재 시간: ");
document.write(current.getHours(),"시");
document.write(current.getMinutes(),"분");
document.write(current.getSeconds(), "초<br>");

</script>
</body>


<script type="text/javascript">

생성1
const now = new Date();

생성2
const now2 = new Date(2022,11,31);

생성3
const now3 = new Date();

now3.setFullYear(2022);
now3.setMonth(11);
now3.setDate(31);
now3.setHours(13);
now3.setMinutes(30);
now3.setSeconds(40);

document.write("<img src=https://media.bunjang.co.kr/product/165951534_1_1633222776_w%7Bres%7D.jpg width='300px' height='100%'>");
document.write("<br>약속날짜: " + now3);

</script>