1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
---
title: Date
slug: Web/JavaScript/Reference/Global_Objects/Date
tags:
- Constructor
- Date
- JavaScript
- Reference
- 날짜
- 시간
translation_of: Web/JavaScript/Reference/Global_Objects/Date
---
<div>{{JSRef}}</div>
<p><span class="seoSummary"><strong><code>Date</code></strong> 생성자는 시간의 특정 지점을 나타내는 <code>Date</code> 객체를 생성합니다.</span> <code>Date</code> 객체는 1970년 1월 1일 UTC(국제표준시) 00:00으로부터 지난 시간을 밀리초로 나타내는 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16">유닉스 타임스탬프</a>를 사용합니다.</p>
<div>{{EmbedInteractiveExample("pages/js/date-constructor.html")}}</div>
<h2 id=".EC.83.9D.EC.84.B1" name=".EC.83.9D.EC.84.B1"><code>Date</code> 객체 초기화</h2>
<p>JavaScript Date 객체를 생성하는 법은 {{jsxref("new")}} 연산자를 사용하는 것이 유일합니다.</p>
<pre class="brush: js">let now = new Date();</pre>
<p>단순히 {{jsxref("Date", "Date")}} 객체를 직접 호출했을 때, 반환 값은 <code>Date</code> 객체가 아니라 날짜를 나타낸 문자열입니다. JavaScript는 <code>Date</code> 리터럴 구문이 없습니다.</p>
<h2 id=".EC.83.9D.EC.84.B1" name=".EC.83.9D.EC.84.B1">구문</h2>
<pre class="syntaxbox">new Date();
new Date(<var>value</var>);
new Date(<var>dateString</var>);
new Date(<var>year</var>, <var>monthIndex</var>[, <var>day</var>[, <var>hour</var>[, <var>minutes</var>[, <var>seconds</var>[, <var>milliseconds</var>]]]]]);
</pre>
<h3 id=".ED.8C.8C.EB.9D.BC.EB.AF.B8.ED.84.B0" name=".ED.8C.8C.EB.9D.BC.EB.AF.B8.ED.84.B0">매개변수</h3>
<p><code>Date()</code> 생성자는 네 가지 형태로 사용할 수 있습니다.</p>
<h4 id="매개변수_없음">매개변수 없음</h4>
<p>매개변수를 제공하지 않으면, 현지 시간으로 생성 순간의 날짜와 시간을 나타내는 <code>Date</code> 객체를 생성합니다.</p>
<h4 id="유닉스_타임스탬프">유닉스 타임스탬프</h4>
<dl>
<dt><code>value</code></dt>
<dd><a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16">유닉스 타임스탬프</a>, 즉 1970년 1월 1일 00:00:00 UTC(유닉스 시간)부터의 시간을 밀리초 단위로 표현하되 윤초는 무시한 정숫값. 대부분의 유닉스 타임스탬프 함수는 초 단위까지만 정확함을 유의하세요.</dd>
<dt>
<h4 id="타임스탬프_문자열">타임스탬프 문자열</h4>
</dt>
<dt><code>dateString</code></dt>
<dd>날짜를 표현하는 문자열값. {{jsxref("Date.parse()")}} 메서드가 인식할 수 있는 형식(<a href="http://tools.ietf.org/html/rfc2822#page-14">IETF 호환 RFC 2822 타임스탬프</a>와 <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15">ISO8601의 한 버전</a>)이어야 합니다
<div class="note"><strong>참고</strong>: <code>Date</code> 생성자(와 <code>Date.parse</code>)를 사용해 날짜 문자열에서 시간을 알아내는건 하지 않는 것이 좋습니다. 브라우저 간 차이와 여러 비일관성이 존재하며 RFC 2822 규격 문자열의 지원은 관례일 뿐입니다. ISO 8601 규격 문자열은 시간 정보가 없을 때("1970-01-01" 등) 현지 시간이 아닌 UTC 기준으로 처리합니다.</div>
</dd>
</dl>
<h4 id="개별_날짜_및_시간_구성_요소">개별 날짜 및 시간 구성 요소</h4>
<p>적어도 연도와 월이 주어지면, 자신의 구성 요소(연, 월, 일, 시, 분, 초, 밀리초)를 모두 매개변수의 값에서 가져오는 <code>Date</code> 객체를 생성합니다. 누락한 요소에는 가장 낮은 값(<code>day</code>는 1, 나머지는 0)을 사용합니다.</p>
<dl>
<dt><code>year</code></dt>
<dd>연도를 나타내는 정숫값. 0부터 99는 1900부터 1999로 처리합니다. {{anch("두 자리 연도는 1900년대로", "아래 예제")}}를 참고하세요.</dd>
<dt><code>monthIndex</code></dt>
<dd>월을 나타내는 정숫값. 0은 1월을 나타내고 11은 12월을 나타냅니다.</dd>
<dt><code>day</code> {{optional_inline}}</dt>
<dd>일을 나타내는 정숫값. 기본값은 1입니다.</dd>
<dt><code>hours</code> {{optional_inline}}</dt>
<dd>시를 나타내는 정숫값. 기본값은 0(자정)입니다.</dd>
</dl>
<dl>
<dt><code>minutes</code> {{optional_inline}}</dt>
<dd>분을 나타내는 정숫값. 기본값은 0분입니다.</dd>
<dt><code>seconds</code> {{optional_inline}}</dt>
<dd>초를 나타내는 정숫값. 기본값은 0초입니다.</dd>
<dt><code>milliseconds</code> {{optional_inline}}</dt>
<dd>밀리초를 나타내는 정숫값. 기본값은 0밀리초입니다.</dd>
</dl>
<h2 id="설명">설명</h2>
<h3 id="유닉스_시간과_타임스탬프">유닉스 시간과 타임스탬프</h3>
<p>JavaScript 날짜는 1970년 1월 1일 자정 (UTC)로부터 지난 시간을 밀리초 단위로 나타낸 것입니다. 이 날짜와 시간을 합쳐 <strong>유닉스 시간</strong>이라고 부릅니다. 유닉스 시간은 컴퓨터로 날짜와 시간을 기록할 때 널리 사용하는 기준점입니다.</p>
<div class="blockIndicator note">
<p><strong>참고</strong>: 시간과 날짜는 현시 시간으로 저장된다는 점을 기억하는게 중요합니다. 시간, 날짜, 아니면 각각의 구성 요소를 회수하는 기본 메서드도 현지 시간에서 동작합니다.</p>
</div>
<p>하루는 86,400,000 밀리초입니다. 타임스탬프 기록에 사용하는 수의 크기를 고려했을 때, Date 객체는 유닉스 시간으로부터 약 ±100,000,000일 (1억일)을 기록할 수 있습니다. 따라서 293,742년이 오면 문제가 발생할 여지가 있습니다.</p>
<h3 id="날짜_형식과_시간대_변환">날짜 형식과 시간대 변환</h3>
<p>날짜를 얻는 방법과 시간대를 바꾸는 메서드는 여럿 있습니다. 그 중 특히 유용한 함수는 날짜 및 시간을 국제 표준 시간인 협정 표준시(UTC)로 반환하는 함수입니다. UTC는 그리니치 시간대라고도 불리는데, 기준 시간대가 영국의 런던과 인근의 그리니치를 지나는 경선이기 때문입니다. 사용자의 장치는 현지 시간을 제공합니다.</p>
<p>{{jsxref("Date.getDay", "getDay()")}}와 {{jsxref("Date.setHours", "setHours()")}}처럼 현지 시간을 기준으로 한 구성요소 읽기 및 쓰기 메서드처럼, {{jsxref("Date.getDayUTC", "getUTCDay()")}} 와 {{jsxref("Date.setHoursUTC", "setUTCHours()")}}처럼 UTC를 기준으로 하는 읽기 및 쓰기 메서드도 존재합니다.</p>
<h2 id="속성">속성</h2>
<dl>
<dt>{{jsxref("Date.prototype")}}</dt>
<dd><code>Date</code> 객체에 추가 속성을 부여할 수 있습니다.</dd>
<dt><code>Date.length</code></dt>
<dd><code>Date.length</code>의 값은 7로, 생성자가 받을 수 있는 매개변수의 수입니다. 보통 유용하게 쓸 수 있는 값은 아닙니다.</dd>
</dl>
<h2 id="메서드">메서드</h2>
<dl>
<dt>{{jsxref("Date.now()")}}</dt>
<dd>1970년 1월 1일 00:00:00 UTC(유닉스 시간)부터 지난 시간을 밀리초 단위의 숫자 값으로 반환합니다. 윤초는 무시합니다.</dd>
<dt>{{jsxref("Date.parse()")}}</dt>
<dd>Parses a string representation of a date and returns the number of milliseconds since 1 January, 1970, 00:00:00, UTC.
<div class="note"><strong>Note:</strong> Parsing of strings with <code>Date.parse</code> is strongly discouraged due to browser differences and inconsistencies.</div>
</dd>
<dt>{{jsxref("Date.UTC()")}}</dt>
<dd>Accepts the same parameters as the longest form of the constructor (i.e. 2 to 7) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored.</dd>
</dl>
<h2 id="Date_instances" name="Date_instances">JavaScript <code>Date</code> 인스턴스</h2>
<p>All <code>Date</code> instances inherit from {{jsxref("Date.prototype")}}. The prototype object of the <code>Date</code> constructor can be modified to affect all <code>Date</code> instances.</p>
<h3 id="Date.prototype_메서드"><code>Date.prototype</code> 메서드</h3>
<div>{{page('/ko/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype', 'Methods')}}</div>
<h2 id=".EC.98.88.EC.A0.9C" name=".EC.98.88.EC.A0.9C">예제</h2>
<h3 id=".EC.98.88.EC.A0.9C:_.EB.82.A0.EC.A7.9C.EB.A5.BC_.EC.A7.80.EC.A0.95.ED.95.98.EB.8A.94_.EC.97.AC.EB.9F.AC.EA.B0.80.EC.A7.80_.EB.B0.A9.EB.B2.95" name=".EC.98.88.EC.A0.9C:_.EB.82.A0.EC.A7.9C.EB.A5.BC_.EC.A7.80.EC.A0.95.ED.95.98.EB.8A.94_.EC.97.AC.EB.9F.AC.EA.B0.80.EC.A7.80_.EB.B0.A9.EB.B2.95">날짜를 지정하는 여러가지 방법</h3>
<p>아래 예제는 날짜를 지정하는 몇가지 방법을 보여줍니다:</p>
<div class="blockIndicator note">
<p><strong>Note:</strong> parsing of date strings with the <code>Date</code> constructor (and <code>Date.parse</code>, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.</p>
</div>
<pre class="brush: js">var today = new Date();
var birthday = new Date("December 17, 1995 03:24:00");
var birthday = new Date(95,11,17);
var birthday = new Date(95,11,17,3,24,0);
</pre>
<h3 id="두_자리_연도는_1900년대로">두 자리 연도는 1900년대로</h3>
<p>In order to create and get dates between the years 0 and 99 the {{jsxref("Date.prototype.setFullYear()")}} and {{jsxref("Date.prototype.getFullYear()")}} methods should be used.</p>
<pre class="brush: js">var date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
// Deprecated method, 98 maps to 1998 here as well
date.setYear(98); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
date.setFullYear(98); // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)
</pre>
<h3 id=".EC.98.88.EC.A0.9C:_.EA.B2.BD.EA.B3.BC.EC.8B.9C.EA.B0.84_.EA.B3.84.EC.82.B0" name=".EC.98.88.EC.A0.9C:_.EA.B2.BD.EA.B3.BC.EC.8B.9C.EA.B0.84_.EA.B3.84.EC.82.B0">경과시간 계산</h3>
<p>The following examples show how to determine the elapsed time between two JavaScript dates in milliseconds.</p>
<p>Due to the differing lengths of days (due to daylight saving changeover), months and years, expressing elapsed time in units greater than hours, minutes and seconds requires addressing a number of issues and should be thoroughly researched before being attempted.</p>
<pre class="brush: js">// Date 객체 사용
var start = Date.now();
// 시간이 오래 걸리는 작업을 여기 배치합니다
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // 경과 시간, 밀리초.
</pre>
<pre class="brush: js">// 내장 메서드 사용
var start = new Date();
// 시간이 오래 걸리는 작업을 여기 배치합니다.
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // 경과 시간, 밀리초.
</pre>
<pre class="brush: js"><code>// to test a function and get back its return
function printElapsedTime(fTest) {
var nStartTime = Date.now(),
vReturn = fTest(),
nEndTime = Date.now();
console.log('Elapsed time: ' + String(nEndTime - nStartTime) + ' milliseconds');
return vReturn;
}
var yourFunctionReturn = printElapsedTime(yourFunction);</code></pre>
<div class="blockIndicator note"><strong>Note:</strong> In browsers that support the {{domxref("window.performance", "Web Performance API", "", 1)}}'s high-resolution time feature, {{domxref("Performance.now()")}} can provide more reliable and precise measurements of elapsed time than {{jsxref("Date.now()")}}.</div>
<h3 id="Get_the_number_of_seconds_since_Unix_Epoch">Get the number of seconds since Unix Epoch</h3>
<pre class="brush: js">var seconds = Math.floor(Date.now() / 1000);</pre>
<p>In this case it's important to return only an integer (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses {{jsxref("Math.floor()")}} and not {{jsxref("Math.round()")}}).</p>
<h2 id="명세">명세</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-date-objects', 'Date')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-date-objects', 'Date')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.9', 'Date')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.1.</td>
</tr>
</tbody>
</table>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<p>{{Compat("javascript.builtins.Date", 3)}}</p>
|