[JavaScript] Date 생성자
태그: javascript · Date · now · parse · UTC · getFullYear setFullYear · getMonth · setMonth · getDate · setDate · getDay · getHours · setHours · getMinutes · setMinutes · getSeconds · setSeconds · getTime · setTime · getTimezoneOffset · toDateString · 날짜 계산
1. Date Constructor
Date
생성자는 시간의 특정 지점을 나타내는 Date
객체를 플랫폼에 종속되지 않는형태로 생성
[Date() 생성자 - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date/Date) |
1) new Date()
인수를 전달하지 않으면 현재 날짜와 시간을 가지는 인스턴스를 반환한다.
const date = new Date();
console.log(date);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T12:05:40.588Z
[Done] exited with code=0 in 0.218 seconds
(1) : new Date(milliseconds) : 1970년 1월 1일 00:00(UTC)을 기점으로 인수로 전달된 밀리초만큼 경과한 날짜와 시간을 가지는 인스턴스를 반환
let date = new Date(0); // 1970년 1월 1일 00:00(UTC) 기점에서 0초 경과
console.log(date);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
1970-01-01T00:00:00.000Z
[Done] exited with code=0 in 0.048 seconds
(2) : new Date(dateString) : 인수로 날짜와 시간을 나타내는 문자열을 전달하면 지정된 날짜와 시간을 가지는 인스턴스 반환
let date = new Date('April 08, 2023 21:07:10');
console.log(date);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T21:07:10.000Z
[Done] exited with code=0 in 0.049 seconds
(3) : new Date(year, month[, day, hour, minute, second, millisecond]) : 년, 월, 일, 시, 분, 초, 밀리초를 의미하는 숫자를 전달하면 지정된 날짜와 시간을 가지는 인스턴스를 반환
year | 1900년 이후의 년 |
---|---|
month | 월을 나타내는 0 ~ 11까지의 정수 (주의: 0부터 시작, 0 = 1월) |
day | 일을 나타내는 1 ~ 31까지의 정수 |
hour | 시를 나타내는 0 ~ 23까지의 정수 |
minute | 분을 나타내는 0 ~ 59까지의 정수 |
second | 초를 나타내는 0 ~ 59까지의 정수 |
millisecond | 밀리초를 나타내는 0 ~ 999까지의 정수 |
let date = new Date('2023/4/08/21:09:30:10');
console.log(date);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T21:09:30.010Z
[Done] exited with code=0 in 0.048 seconds
- Date Method
(1) : Date.now : 1970년 1월 1일 00:00:00(UTC)을 기점으로 현재 시간까지 경과한 밀리초를 숫자로 반환
(2) : Date.parse : 1970년 1월 1일 00:00:00(UTC)을 기점으로 인수로 전달된 지정 시간(new Date(dateString)의 인수와 동일한 형식)까지의 밀리초를 숫자로 반환
let date = Date.parse('April 16, 2023 21:07:10');
console.log(date);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
1681679230000
[Done] exited with code=0 in 0.048 seconds
(3) :Date.UTC : 1970년 1월 1일 00:00:00(UTC)을 기점으로 인수로 전달된 지정 시간까지의 밀리초를 숫자로 반환 : 년, 월, 일, 시, 분, 초, 밀리초를 의미하는 숫자를 전달하면 지정된 날짜와 시간을 가지는 인스턴스를 반환
let d = Date.UTC(1970, 0, 2);
console.log(d);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
1681679230000
[Done] exited with code=0 in 0.049 seconds
(4) : Date.prototype.getFullYear / Date.prototype.setFullYear : 년도를 나타내는 4자리 숫자를 반환/설정
const today = new Date();
console.log(today);
console.log(today.getFullYear());
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T12:12:37.104Z
2023
[Done] exited with code=0 in 0.056 seconds
(5) : Date.prototype.getMonth / Date.prototype.setMonth : 월을 나타내는 0 ~ 11의 정수를 반환/설정한다. 1월은 0, 12월은 11
const today = new Date();
// 월/일을 지정
today.setMonth(10, 5);
console.log(today);
console.log(today.getMonth());
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-11-05T12:14:59.830Z
10
[Done] exited with code=0 in 0.049 seconds
(6) : Date.prototype.getDate / Date.prototype.setDate : 날짜(1 ~ 31)를 나타내는 정수를 반환/설정
const today = new Date();
today.setDate(1);
console.log(today);
console.log(today.getDate());
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-01T12:16:09.444Z
1
[Done] exited with code=0 in 0.111 seconds
(7) : Date.prototype.getDay : 요일(0 ~ 6)를 나타내는 정수를 반환
요일 | 반환값 |
---|---|
일요일 | 0 |
월요일 | 1 |
화요일 | 2 |
수요일 | 3 |
목요일 | 4 |
금요일 | 5 |
토요일 | 6 |
(8) : Date.prototype.getHours / Date.prototype.setHours : 시간(0 ~ 23) 를 나타내는 정수를 반환/설정
const today = new Date();
today.setHours(7);
console.log(today);
console.log(today.getHours()); // 7
// 시간/분/초/밀리초 지정
today.setHours(0, 0, 0, 0); // 00:00:00:00
console.log(today);
console.log(today.getHours()); // 0
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T07:17:14.809Z
7
2023-04-08T00:00:00.000Z
0
[Done] exited with code=0 in 0.055 seconds
(9) : Date.prototype.getMinutes / Date.prototype.setMinutes : 분(0 ~ 59)를 나타내는 정수를 반환/설정
let today = new Date();
// 분 지정
today.setMinutes(50);
console.log(today);
console.log(today.getMinutes()); // 50
// 분/초/밀리초 지정
today.setMinutes(5, 10, 999); // HH:05:10:999
console.log(today);
console.log(today.getMinutes()); // 5
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T12:50:10.716Z
50
2023-04-08T12:05:10.999Z
5
[Done] exited with code=0 in 0.048 seconds
(10) : Date.prototype.getSeconds / Date.prototype.setSeconds : 시간(0~ 23)를 나타내는 정수를 반환/설정
let today = new Date();
// 분 지정
today.setSeconds(30);
console.log(today);
console.log(today.getSeconds()); // 30
// 초/밀리초 지정
today.setSeconds(10, 0); // HH:MM:10:000
console.log(today);
console.log(today.getSeconds()); // 10
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T12:18:30.029Z
30
2023-04-08T12:18:10.000Z
10
[Done] exited with code=0 in 0.052 seconds
(11) : Date.prototype.getTime / Date.prototype.setTime : 1970년 1월 1일 00:00:00(UTC)를 기점으로 현재 시간까지 경과된 밀리초를 반환/설정
const today = new Date(); // Fri Jan 01 1970 09:00:00 GMT+0900 (한국 표준시)
// 1970년 1월 1일 00:00:00(UTC)를 기점으로 현재 시간까지 경과된 밀리초 지정
today.setTime(86400000);
console.log(today);
console.log(today.getTime()); // 86400000
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
1970-01-02T00:00:00.000Z
86400000
[Done] exited with code=0 in 0.048 seconds
(12) : Date.prototype.getTimezoneOffset : UTC와 지정 로케일(Locale) 시간과의 차이를 분단위로 반환(KST(Korea Standard Time)는 UTC에 9시간을 더한 시간)
const today = new Date();
const x = today.getTimezoneOffset() / 60; // 분단위값을 60으로 나눈다
console.log(today);
console.log(x);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
2023-04-08T12:21:37.497Z
0
[Done] exited with code=0 in 0.049 seconds
(13) : Date.prototype.toDateString : 사람이 읽을 수 있는 형식의 문자열로 날짜를 반환
const d = new Date('2023/4/08/21:30');
console.log(d.toString());
console.log(d.toDateString())
=>결과값
[Running] node "/workspaces/codespaces-blank/index.js"
Sat Apr 08 2023 21:30:00 GMT+0000 (Coordinated Universal Time)
Sat Apr 08 2023
[Done] exited with code=0 in 0.048 seconds
(14) : Date.prototype.toTimeString : 사람이 읽을 수 있는 형식의 문자열로 시간을 반환
const d = new Date('2023/4/08/21:30');
console.log(d.toString());
console.log(d.toTimeString());
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
Sat Apr 08 2023 21:30:00 GMT+0000 (Coordinated Universal Time)
21:30:00 GMT+0000 (Coordinated Universal Time)
[Done] exited with code=0 in 0.049 seconds
2) 날짜 계산 방법
(1) 몇일 전, 몇일 후 날짜 계산
var now = new Date(); // 현재 날짜 및 시간
console.log("현재 : ", now);
var yesterday = new Date(now.setDate(now.getDate() - 1)); // 어제
console.log("어제 : ", yesterday);
var tomorrow = new Date(now.setDate(now.getDate() + 1)); // 내일
console.log("내일 : ", tomorrow);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
현재 : 2023-04-08T12:24:55.006Z
어제 : 2023-04-07T12:24:55.006Z
내일 : 2023-04-08T12:24:55.006Z
[Done] exited with code=0 in 0.052 seconds
(2) 몇달 전, 몇달 후 날짜 계산
var now = new Date(); // 현재 날짜 및 시간
console.log("현재 : ", now);
var oneMonthAgo = new Date(now.setMonth(now.getMonth() - 1)); // 한달 전
console.log("한달 전 : ", oneMonthAgo);
var oneMonthLater = new Date(now.setMonth(now.getMonth() + 2)); // 한달 후
console.log("한달 후 : ", oneMonthLater);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
현재 : 2023-04-08T12:26:36.280Z
한달 전 : 2023-03-08T12:26:36.280Z
한달 후 : 2023-05-08T12:26:36.280Z
[Done] exited with code=0 in 0.05 seconds
(3) 1년 전, 1년 후 날짜 계산
var now = new Date(); // 현재 날짜 및 시간
console.log("현재 : ", now);
var oneYearsAgo = new Date(now.setFullYear(now.getFullYear() - 1)); // 1년 전
console.log("1년 전 : ", oneYearsAgo);
var oneYearsLater = new Date(now.setFullYear(now.getFullYear() + 2)); // 1년 후
console.log("1년 후 : ", oneYearsLater);
=> 결과값
[Running] node "/workspaces/codespaces-blank/index.js"
현재 : 2023-04-08T12:29:01.710Z
1년 전 : 2022-04-08T12:29:01.710Z
1년 후 : 2024-04-08T12:29:01.710Z
[Done] exited with code=0 in 0.05 seconds