aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/date/tostring/index.html
blob: ee29941d48ad5c448f00e92ce6749e5f3c46a6fb (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
---
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>

<div>{{EmbedInteractiveExample("pages/js/date-tostring.html")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate"><var>dateObj</var>.toString()</pre>

<h3 id="Return_value" name="Return_value">返値</h3>

<p>与えられた日付を表す文字列。</p>

<h2 id="Description" name="Description">解説</h2>

<p>{{jsxref("Date")}} のインスタンスは <code>toString()</code> メソッドを {{jsxref("Date.prototype")}} から継承しており、 {{jsxref("Object.prototype")}} から継承しているわけではありません。 <code>Date.prototype.toString()</code> は Date を表す文字列を、 ECMA-262 で指定された以下のような書式で返します。</p>

<ul>
 <li>曜日: 3文字の英語の曜日名。例 "Sat"</li>
 <li>空白</li>
 <li>月名: 3文字の英語の月名。例 "Sep"</li>
 <li>空白</li>
 <li>日: 2桁の日。例 "01"</li>
 <li>空白</li>
 <li>年: 4桁の年。例 "2018"</li>
 <li>空白</li>
 <li>時: 2桁の時。例 "14"</li>
 <li>コロン</li>
 <li>分: 2桁の分。例 "53"</li>
 <li>コロン</li>
 <li>秒: 2桁の秒。例 "26"</li>
 <li>空白</li>
 <li>文字列 "GMT"</li>
 <li>タイムゾーンのオフセット記号。以下のどちらかです。
  <ul>
   <li>"+" 正のオフセット (0以上)</li>
   <li>"-" 負のオフセット (0未満)</li>
  </ul>
 </li>
 <li>2桁の時間のオフセット。例 "14"</li>
 <li>2桁の分のオフセット。例 "00"</li>
 <li>任意で、以下の形のタイムゾーン名。
  <ul>
   <li>空白</li>
   <li>左括弧、すなわち "("</li>
   <li>タイムゾーンを表す実装依存の文字列で、省略形の場合も完全な名前の場合もあります (タイムゾーンに名前や省略形の標準はありません。例 "Line Islands Time" または "LINT"</li>
   <li>右括弧、すなわち ")"</li>
  </ul>
 </li>
</ul>

<p>例 "Sat Sep 01 2018 14:53:26 GMT+1400 (LINT)"</p>

<p>ECMAScript 2018 (第9編) まで、 <code>Date.prototype.toString</code> が返す文字列の書式は実装に依存していました。したがって、指定された書式通りであることに頼ってはいけません。</p>

<p><code>toString()</code> メソッドは、日付がテキスト値で表現されるとき、例えば <code>console.log(new Date())</code>、または日付が文字列に強制変換されるとき、例えば <code>var today = 'Today is ' + new Date()</code> などで自動的に呼び出されます。</p>

<p><code>toString()</code> は汎用メソッドです。 <code>this</code>{{jsxref("Date")}} インスタンスある必要はありません。しかし、ネイティブの JavaScript を使用して構築することができない内部の <code>[[TimeValue]]</code> プロパティを持っている必要があるため、事実上は {{jsxref("Date")}} インスタンスでの使用に限定されています。日付でないインスタンスで呼び出された場合、 {{jsxref("TypeError")}} が発生します。</p>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Using_toString" name="Using_toString">toString() の使用</h3>

<p>次の例は、{{jsxref("Date")}} オブジェクトの <code>toString()</code> 値を <code>myVar</code> に代入します。</p>

<pre class="brush: js notranslate">var x = new Date();
var myVar = x.toString(); // 次のような値を myVar に代入します:
                          // Mon Sep 08 1998 14:36:22 GMT-0700 (PDT)
</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-date.prototype.tostring', 'Date.prototype.toString')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<p>{{Compat("javascript.builtins.Date.toString")}}</p>

<h2 id="See_also" name="See_also">関連情報</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>