본문 바로가기
JavaScript

[JavaScript] Date

by eddypark 2023. 10. 24.

- 날짜와 시간을 위한 메서드를 제공하는 빌트인 객체이면서 생성자 함수

(1) Date Constructor

- new Date() : 인수를 전달하지 않으면 현재 날짜와 시간을 가지는 객체를 반환한다.

- new Date(milliseconds) : 1970년 1월 1일 을 기점으로 전달된 밀리초만큼 경과한 날짜와 시간을 가지는 객체를 반환한다.

- new Date(String) : 인수로 전달한 날짜와 시간을 가지는 객체로 반환한다. 단 문자열은 Data.parse 메서드에 의해 해석이 가능한 형식이어야 한다.

- new Date(year, month, day, hour, minute, second, millisecond) : 날짜와 시간을 인수로 지정할 수 있으며 반드시 년, 월은 지정해야 한다.(month는 0부터 1월)

- 지정하지 않은 옵션은 0이나 1로 초기화된다.

- new 없이 호출하면 결괏값을 문자열로 반환한다.

var a = new Date();
console.log(a);                              // Thu May 16 2019 17:16:13 GMT+0900 (한국 표준시)

var b = new Date(0);
console.log(b);                              // Thu Jan 01 1970 09:00:00 GMT+0900 (한국 표준시)

b = new Date(86400000);                      // 하루는 86400초, 86400000밀리초
console.log(b);

var c = new Date('May 16, 2019 17:22:10');
console.log(c);                              // Thu May 16 2019 17:22:10 GMT+0900 (한국 표준시)

c = new Date('2019/05/16/17:22:10');
console.log(c);                              // Thu May 16 2019 17:22:10 GMT+0900 (한국 표준시)

var d = new Date(2019, 4);
console.log(d);                              // Wed May 01 2019 00:00:00 GMT+0900 (한국 표준시)

d = new Date(2019, 4, 16, 17, 24, 30, 0);    // 2019/5/16/17:24:30:00
console.log(d);                              // Thu May 16 2019 17:24:30 GMT+0900 (한국 표준시)

d = new Date('2019/5/16/17:24:30:10');
console.log(d);                              // Thu May 16 2019 17:24:30 GMT+0900 (한국 표준시)

var e = Date();
console.log(typeof e, e);                    // string Thu May 16 2019 17:33:03 GMT+0900 (한국 표준시)

(2) Date Method

- Date.now() : 1970년 1월 1일 을 기점으로 현재 시간까지 경과한 밀리초를 숫자로 반환한다.

- Date.parse() : 1970년 1월 1일 을 기점으로 인수로 전달된 지정시간까지의 밀리초를 숫자로 반환한다.

- Date.UTC() : 1970년 1월 1일 을 기점으로 인수로 전달된 지정시간까지의 밀리초를 숫자로 반환한다. 인수는 UTC로 인식된다.

var a = Date.now();
console.log(a);

var b = Date.parse('Jan 2, 1970 00:00:00 UTC'); 
console.log(b); // 86400000

b = Date.parse('1970/01/02/09:00:00');
console.log(b); // 86400000

var c = Date.UTC(1970, 0, 2);
console.log(c); // 86400000

c = Date.UTC('1970/1/2');
console.log(c); // NaN

- Date.prototype.getFullYear() : 년도를 나타내는 4자리 숫자를 반환한다.

- Date.prototype.setFullYear() : 년도를 나타내는 4자리 숫자를 설정한다. 월, 일도 설정 가능하다.

var a = new Date();
var b = a.getFullYear();

console.log(a); // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  // 2019

a.setFullYear(2000);
b = a.getFullYear();
console.log(a); // Tue May 16 2000 17:42:40 GMT+0900 (한국 표준시)
console.log(b);  // 2000

- Date.prototype.getMonth() : 월을 나타내는 0 ~ 11의 정수를 반환한다. (0부터 1월)

- Date.prototype.setMonth() : 월을 나타내는 0 ~ 11의 정수를 설정한다. 일도 설정 가능하다.

var a = new Date();
var b = a.getMonth();

console.log(a); // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  // 4

a.setMonth(0);
b = a.getMonth();
console.log(a); // Tue Jan 16 2019 17:42:40 GMT+0900 (한국 표준시)
console.log(b);  // 0

- Date.prototype.getDate() : 날짜를 나타내는 정수를 반환한다.

- Date.prototype.setDate() : 날짜를 나타내는 정수를 설정한다.

- Date.prototype.getDay() : 요일을 나타내는 정수를 반환한다. (일요일부터 0 월요일 1 .....)

var a = new Date();
var b = a.getDate();

console.log(a); // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  // 16

a.setDate(1);
b = a.getDate();
console.log(a); // Tue May 16 2019 17:42:40 GMT+0900 (한국 표준시)
console.log(b);  // 1

var c = a.getDay();
console.log(a); // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(c); // 4

- Date.prototype.getHour() : 시간을 나타내는 정수를 반환한다.

- Date.prototype.setHour() : 시간을 나타내는 정수를 설정한다. 분, 초, 밀리초도 설정할 수 있다.

var a = new Date();
var b = a.getHours();

console.log(a);  // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  // 17

a.setHours(7);
b = a.getHours();
console.log(a);  // Tue May 16 2019 07:42:40 GMT+0900 (한국 표준시)
console.log(b);  // 7

- Date.prototype.getMinutes() : 분을 나타내는 정수를 반환한다.

- Date.prototype.setMinutes() : 분을 나타내는 정수를 설정한다. 초, 밀리초도 설정할 수 있다.

var a = new Date();
var b = a.getMinutes();

console.log(a);  // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  // 39

a.setMinutes(50);
b = a.getMinutes();
console.log(a);  // Tue May 16 2019 17:50:40 GMT+0900 (한국 표준시)
console.log(b);  // 50

- Date.prototype.getSeconds() : 초를 나타내는 정수를 반환한다.

- Date.prototype.setSeconds() : 초를 나타내는 정수를 설정한다. 밀리초도 설정할 수 있다.

var a = new Date();
var b = a.getSeconds();

console.log(a);  // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  // 30

a.setSeconds(40);
b = a.getSeconds();
console.log(a);  // Tue May 16 2019 17:39:40 GMT+0900 (한국 표준시)
console.log(b);  // 40

- Date.prototype.getMilliseconds : 밀리초를 나타내는 정수를 반환한다.

- Date.prototype.setMilliseconds : 밀리초를 나타내는 정수를 설정한다.

var a = new Date();
var b = a.getMilliseconds();

console.log(a); // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  

a.setMilliseconds(40);
b = a.getMilliseconds();
console.log(a); // Tue May 16 2019 17:39:40 GMT+0900 (한국 표준시)
console.log(b);

- Date.prototype.getTime : 1970년 1월 1일 00:00:00을 기점으로 현재 시간까지 경과된 밀리초를 반환한다.

- Date.prototype.setTime : 1970년 1월 1일 00:00:00(UTC)을 기점으로 현재 시간까지 경과된 밀리초를 설정한다.

var a = new Date();
var b = a.getTime();

console.log(a);  // Thu May 16 2019 17:39:30 GMT+0900 (한국 표준시)
console.log(b);  

a.setTime(86400000);
b = a.getTime();
console.log(a);  // Tue May 16 2019 17:39:40 GMT+0900 (한국 표준시)
console.log(b);  //86400000

- Date.prototype.toDateString : 사람이 읽을 수 있는 형식의 문자열로 날짜를 반환한다.

- Date.prototype.toTimeString : 사람이 읽을 수 있는 형식의 문자열로 시간을 반환한다.

var a = new Date('2019/5/16/18:30');

console.log(a.toString());     // Thu May 16 2019 18:30:00 GMT+0900 (한국 표준시)
console.log(a.toDateString()); // Thu May 16 2019

console.log(a.toString());     // Thu May 16 2019 18:30:00 GMT+0900 (한국 표준시)
console.log(a.toTimeString()); // 18:30:00 GMT+0900 (한국 표준시)

'JavaScript' 카테고리의 다른 글

[JavaScript] String  (0) 2023.10.26
[JavaScript] 정규표현식  (1) 2023.10.24
[JavaScript] Math  (0) 2023.10.20
[JavaScript]Number  (1) 2023.10.20
[JavaScript]Global Object  (1) 2023.10.19