본문 바로가기
Web/JavaScript

[JavaScript] 객체 - 3️⃣Date 객체 & Date 객체 함수

by coding-choonsik 2023. 4. 11.
728x90
반응형
SMALL

1. Date 객체

  • 날짜, 시간 등을 쉽게 처리할 수 있는 내장 객체
  • 날짜와 시간 등 구성요소를 가져오는 메서드는 모두 현지(호스트 시스템의 위치)의 시간대 사용

 

1-1. Date 객체 생성방법

  • new Date(): 현재 날짜 시간을 저장한 객체가 생성
  • new Date('날짜 문자열'): 해당 특정 날짜와 시간을 저장한 객체가 생성
  • new Date('밀리초'): 1970년 1월 1일 0시 0분 0초를 기준으로 해당 밀리초만큼 지난 날짜와 시간을 저장한 객체가 생성
  • new Date(년, 월, 일, 시, 분, 초, 밀리초): 해당 날짜와 시간을 저장한 객체가 생성

 

1-2. 연도(year)

  • 2자리로 연도를 표기: 1900년 ~ 1999년
  • 4자리로 연도를 표기: 2000년 ~

 

1-3. 월(month)

  •  0 ~ 11월
  •  0은 1월, 11이 12월


2. Date 객체 함수

  • getDate(): 현지 시간 기준 일(1~31)을 반환
  • getDay(): 현지 시간 기준 요일(0~6)을 반환
  • getFullYear(): 현지 시간 기준 연도(네 자리)를 반환
  • getMonth(): 현지 시간 기준 월(0~11)을 반환
  • getHours(): 현지 시간 기준 시(0~23)를 반환
  • getMinutes(): 현지 시간 기준 분(0~59)을 반환
  • getSeconds(): 현지 시간 기준 초(0~59)를 반환
  • getMilliseconds(): 현지 시간 기준 밀리초(0~999)를 반환
  • getTime(): 1970년 1월 1일 00:00:00 UTC로부터의 경과시간을 밀리초 단위로 반환

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Date객체</title>
</head>
<body>
    <h2>Date객체</h2>
    <script>
        console.log(new Date())
        console.log(new Date(23, 4, 6))  // "1923-05-05T15:00:00.000Z"
        const current_time= new Date(2023, 3, 6, 14, 44, 00)
        console.log(current_time)
        
        console.log(`현재 연도: ${current_time.getFullYear()}`)
        console.log(`현재 월: ${current_time.getMonth() + 1}`)
        console.log(`현재 일: ${current_time.getDate()}`)
        console.log(`현재 시간: ${current_time.getHours()}`)
        console.log(`현재 분: ${current_time.getMinutes()}`)
        console.log(`현재 초: ${current_time.getSeconds()}`)

        console.log(new Date(mil))
    </script>
</body>
</html>

 


MDN: Date

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date

 

Date - JavaScript | MDN

JavaScript Date 객체는 시간의 한 점을 플랫폼에 종속되지 않는 형태로 나타냅니다. Date 객체는 1970년 1월 1일 UTC(협정 세계시) 자정과의 시간 차이를 밀리초로 나타내는 정수 값을 담습니다.

developer.mozilla.org

 

 

728x90
반응형
LIST