blob: a624293ff0ae3c3c570e7da5a954ee8b72691f7f (
plain)
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
|
---
title: Date.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Date/toString
tags:
- Date
- JavaScript
- Method
- Prototype
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Date/toString
---
<div>{{JSRef}}</div>
<p> <strong><code>toString()</code></strong> 메서드는 {{jsxref("Date")}} 객체의 시간을 문자열로 반환합니다.</p>
<p>{{EmbedInteractiveExample("pages/js/date-tostring.html")}}</p>
<h2 id="구문">구문</h2>
<pre class="syntaxbox"><var>dateObj</var>.toString()</pre>
<h3 id="반환_값">반환 값</h3>
<p>주어진 날짜를 나타내는 문자열.</p>
<h2 id="설명">설명</h2>
<p>{{jsxref("Date")}} 객체는 <code>toString()</code> 메서드를 {{jsxref("Object.prototype")}}이 아닌 {{jsxref("Date.prototype")}}에서 상속받습니다. <code>Date.prototype.toString()</code>의 반환값은 ECMA-262에 명시되어있으며 다음과 같이 요약할 수 있습니다.</p>
<ul>
<li>요일: 세 글자 영어 요일명. 예: "Sat"</li>
<li>공백</li>
<li>월: 세 글자 영어 월 이름. 예: "Sep"</li>
<li>공백</li>
<li>일: 두 글자 숫자. 예: "01"</li>
<li>공백</li>
<li>연: 네 글자 숫자. 예: "2018"</li>
<li>공백</li>
<li>시: 두 글자 숫자. 예: "14"</li>
<li>콜론</li>
<li>분: 두 글자 숫자. 예: "53"</li>
<li>콜론</li>
<li>초: 두 글자 숫자. 예: "26"</li>
<li>공백</li>
<li>문자열 "GMT"</li>
<li>시간대 차이의 부호.
<ul>
<li>0 이상일 경우 "+"</li>
<li>0 미만일 경우 "-"</li>
</ul>
</li>
<li>두 글자 숫자로 표현한 시 차이. 예: "14"</li>
<li>두 글자 숫자로 표현한 분 차이. 예: "00"</li>
<li>선택사항: 다음으로 구성한 시간대의 이름.
<ul>
<li>공백</li>
<li>여는 소괄호</li>
<li>구현마다 다를 수 있는 시간대 이름. 약어와 전체 이름 둘 다 가능. 예: "Korea Standard Time", "한국 표준시" 또는 "KST"</li>
<li>닫는 소괄호</li>
</ul>
</li>
</ul>
<p>예: "Sat Sep 01 2018 14:53:26 GMT+0900 (KST)"</p>
<p>ECMAScript 2018(제9판) 전까지 <code>toString()</code>의 반환 형식은 구현에 따라 다를 수 있었습니다. 따라서 특정 형식에 의존하지 않아야 합니다.</p>
<p><code>toString()</code> 메서드는 날짜를 문자열로 표현해야 할 때 자동으로 쓰입니다. <code>console.log(new Date())</code>와 <code>const today = 'Today is ' + new Date()</code> 등의 경우를 예로 들 수 있습니다.</p>
<p><code>toString()</code>은 제네릭 메서드로 <code>this</code>의 값이 {{jsxref("Date")}}일 필요는 없습니다. 그러나 내부적으로 <code>[[TimeValue]]</code> 속성이 필요한데, 이는 JavaScript로는 설정할 수 없어 결국 사용이 <code>Date</code> 인스턴스로 제한됩니다. <code>Date</code> 외의 다른 객체에서 호출하면 {{jsxref("TypeError")}}가 발생합니다.</p>
<h2 id="예제">예제</h2>
<h3 id="toString()_사용"><code>toString()</code> 사용</h3>
<pre class="brush: js"><code>var x = new Date();
var myVar = x.toString(); // assigns a string value to myVar in the same format as:
// Mon Sep 08 1998 14:36:22 GMT+0900 (KST)</code></pre>
<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('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.0.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.9.5.2', 'Date.prototype.toLocaleTimeString')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-date.prototype.tostring', 'Date.prototype.toString')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-date.prototype.tostring', 'Date.prototype.toString')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<p>{{Compat("javascript.builtins.Date.toString")}}</p>
<h2 id="같이_보기">같이 보기</h2>
<ul>
<li>{{jsxref("Object.prototype.toString()")}}</li>
<li>{{jsxref("Date.prototype.toDateString()")}}</li>
<li>{{jsxref("Date.prototype.toLocaleString()")}}</li>
<li>{{jsxref("Date.prototype.toTimeString()")}}</li>
</ul>
|