aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/date/totimestring/index.html
blob: b99486c6b68b4789e538cbe87e11b8649481e722 (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
---
title: Date.prototype.toTimeString()
slug: Web/JavaScript/Reference/Global_Objects/Date/toTimeString
translation_of: Web/JavaScript/Reference/Global_Objects/Date/toTimeString
---
<div>{{JSRef("Global_Objects", "Date")}}</div>

<p><code><strong>toTimeString()</strong></code> 方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。</p>

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



<h2 id="Syntax" name="Syntax">语法</h2>

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

<h2 id="Description" name="Description">描述</h2>

<p>{{jsxref("Global_Objects/Date", "Date")}} 对象的实例引用一个具体的时间点。 调用 {{jsxref("Date.toString", "toString")}} 方法以美式英语和人类易读的形式,返回日期对象的格式化字符串。在 <a href="/en-US/docs/SpiderMonkey" title="SpiderMonkey">SpiderMonkey</a> 里,该字符串由日期部分(年月日)和其后的时间部分(时分秒和时区)组成。有时会需要获取时间部分的字符串,这可以由 <code>toTimeString</code> 方法完成。</p>

<p>The <code style="font-style: normal;">toTimeString</code> method is especially useful because compliant engines implementing <a href="https://developer.mozilla.org/en-US/docs/ECMAScript" title="ECMAScript">ECMA-262</a> may differ in the string obtained from <code style="font-style: normal;">toString</code> for <code style="font-style: normal;">Date</code> objects, as the format is implementation-dependent; simple string slicing approaches may not produce consistent results across multiple engines.</p>

<h2 id="Example" name="Example">例子</h2>

<h3 id="Example:_A_basic_usage_of_toTimeString" name="Example:_A_basic_usage_of_toTimeString">例子:<code>toTimeString</code> 方法的简单使用</h3>

<pre class="brush:js">var d = new Date(1993, 6, 28, 14, 39, 7);

println(d.toString());     // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
println(d.toTimeString()); // prints 14:39:07 GMT-0600 (PDT)
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.9.5.4', 'Date.prototype.toTimeString')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-date.prototype.totimestring', 'Date.prototype.toTimeString')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

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

<h2 id="See_Also" name="See_Also">相关链接</h2>

<ul>
 <li>{{jsxref("Date.prototype.toLocaleTimeString()")}}</li>
 <li>{{jsxref("Date.prototype.toDateString()")}}</li>
 <li>{{jsxref("Date.prototype.toString()")}}</li>
</ul>