라이브러리
[JAVASCRIPT] date.getFullYear() - 연도(4자리) 반환
JavaScript의 Date 객체
JavaScript의 Date 객체는 날짜와 시간을 다루기 위한 객체입니다. Date 객체는 `new Date()`를 통해 생성할 수 있으며, 생성된 Date 객체는 날짜와 시간을 나타내는 여러 메서드를 제공합니다.
getFullYear() 메서드
getFullYear() 메서드는 Date 객체의 년도를 반환합니다. 년도는 4자리 숫자로 반환됩니다.
# 예제
#hostingforum.kr
javascript
// 현재 날짜와 시간을 얻기 위해 Date 객체를 생성합니다.
const currentDate = new Date();
// getFullYear() 메서드를 사용하여 년도를 얻습니다.
const year = currentDate.getFullYear();
console.log(year); // 2024
# 년도와 월, 일, 시간, 분, 초를 얻기
#hostingforum.kr
javascript
// 현재 날짜와 시간을 얻기 위해 Date 객체를 생성합니다.
const currentDate = new Date();
// 년도, 월, 일, 시간, 분, 초를 얻습니다.
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // getMonth()는 0부터 시작하므로 1을 더합니다.
const day = currentDate.getDate();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
console.log(`년: ${year}, 월: ${month}, 일: ${day}, 시간: ${hour}, 분: ${minute}, 초: ${second}`);
# 년도와 월, 일, 시간, 분, 초를 얻기 (formatting)
#hostingforum.kr
javascript
// 현재 날짜와 시간을 얻기 위해 Date 객체를 생성합니다.
const currentDate = new Date();
// 년도, 월, 일, 시간, 분, 초를 얻습니다.
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // getMonth()는 0부터 시작하므로 1을 더합니다.
const day = currentDate.getDate();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
// formatting을 위해 template literals를 사용합니다.
console.log(`년: ${year}, 월: ${month}월, 일: ${day}일, 시간: ${hour}시, 분: ${minute}분, 초: ${second}초`);
# 년도와 월, 일, 시간, 분, 초를 얻기 (formatting, locale)
#hostingforum.kr
javascript
// 현재 날짜와 시간을 얻기 위해 Date 객체를 생성합니다.
const currentDate = new Date();
// 년도, 월, 일, 시간, 분, 초를 얻습니다.
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // getMonth()는 0부터 시작하므로 1을 더합니다.
const day = currentDate.getDate();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
// formatting을 위해 template literals를 사용합니다.
console.log(`년: ${year}, 월: ${month}월, 일: ${day}일, 시간: ${hour}시, 분: ${minute}분, 초: ${second}초`);
// locale을 사용하여 formatting을 합니다.
console.log(currentDate.toLocaleString('ko-KR')); // 한국어로 formatting을 합니다.
console.log(currentDate.toLocaleString('en-US')); // 영어로 formatting을 합니다.
이러한 예제를 통해 JavaScript의 Date 객체와 getFullYear() 메서드의 사용법을 이해할 수 있습니다.
댓글목록
등록된 댓글이 없습니다.