aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/date/toisostring/index.html
blob: 34815de3f5c872ad184e3b6e65644debeb33623f (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
---
title: Date.prototype.toISOString()
slug: Web/JavaScript/Reference/Global_Objects/Date/toISOString
tags:
  - Date
  - JavaScript
  - Method
  - Prototype
  - Reference
  - polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Date/toISOString
---
<div>{{JSRef}}</div>

<p><strong><code>toISOString()</code></strong> メソッドは、<em>簡潔な</em>拡張表記の ISO 形式 (<a href="https://ja.wikipedia.org/wiki/ISO_8601">ISO 8601</a>) の文字列を返します。これは、常に 24 文字または 27 文字の長さになります (それぞれ、<code><var>YYYY</var>-<var>MM</var>-<var>DD</var>T<var>HH</var>:<var>mm</var>:<var>ss.sss</var>Z</code> または <code><var>±</var><var>YYYYYY</var>-<var>MM</var>-<var>DD</var>T<var>HH</var>:<var>mm</var>:<var>ss.sss</var>Z</code>)。タイムゾーンは常に 0 UTC オフセットになり、接尾辞 "<code>Z</code>" で表記されます。</p>

<div>{{EmbedInteractiveExample("pages/js/date-toisostring.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>.toISOString()</pre>

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

<p>協定世界時に基づき、与えられた日付を <a href="https://ja.wikipedia.org/wiki/ISO_8601">ISO 8601</a> 形式で表す文字列。</p>

<h2 id="Polyfill" name="Polyfill">ポリフィル</h2>

<p>このメソッドは、ECMA-262 第 5 版で標準化されました。このメソッドに対応するため、更新されていないエンジンでは、次のコードを用いてこのメソッドの欠落を補うことができます。</p>

<pre class="brush: js notranslate">if (!Date.prototype.toISOString) {
  (function() {

    function pad(number) {
      if (number &lt; 10) {
        return '0' + number;
      }
      return number;
    }

    Date.prototype.toISOString = function() {
      return this.getUTCFullYear() +
        '-' + pad(this.getUTCMonth() + 1) +
        '-' + pad(this.getUTCDate()) +
        'T' + pad(this.getUTCHours()) +
        ':' + pad(this.getUTCMinutes()) +
        ':' + pad(this.getUTCSeconds()) +
        '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
        'Z';
    };

  })();
}
</pre>

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

<h3 id="Using_toISOString" name="Using_toISOString">toISOString() の使用</h3>

<pre class="brush: js notranslate">let today = new Date('05 October 2011 14:48 UTC')

console.log(today.toISOString())  // 2011-10-05T14:48:00.000Z を返す
</pre>

<p>上記の例は、Mozilla 以外のブラウザーでは正しく解析されない、非標準の文字列値を解析するのに使います。</p>

<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.toisostring', 'Date.prototype.toISOString')}}</td>
  </tr>
 </tbody>
</table>

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

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

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("Date.prototype.toLocaleDateString()")}}</li>
 <li>{{jsxref("Date.prototype.toTimeString()")}}</li>
 <li>{{jsxref("Date.prototype.toUTCString()")}}</li>
</ul>