diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html new file mode 100644 index 0000000000..3bac439efa --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html @@ -0,0 +1,97 @@ +--- +title: Date.prototype.getTime() +slug: Web/JavaScript/Reference/Global_Objects/Date/getTime +tags: + - Date + - JavaScript + - 原型 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getTime +--- +<div>{{JSRef}}</div> + +<p><code><strong>getTime()</strong></code> 方法返回一个时间的格林威治时间数值。</p> + +<p>你可以使用这个方法把一个日期时间赋值给另一个{{jsxref("Date")}} 对象。这个方法的功能和 {{jsxref("Date.valueof", "valueOf()")}} 方法一样。</p> + +<div>{{EmbedInteractiveExample("pages/js/date-gettime.html")}}</div> + + + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>dateObj.getTime() </code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<p>无。</p> + +<h3 id="Description" name="Description">返回值</h3> + +<p><code>getTime</code> 方法的返回值一个数值,表示从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数。</p> + +<h2 id="Examples" name="Examples">例子</h2> + +<h3 id="使用_getTime()_复制日期对象">使用 getTime() 复制日期对象</h3> + +<p>创建一个拥有相同时间值的日期对象。</p> + +<pre class="brush: js">var birthday = new Date(1991, 9, 17); +var copy = new Date(); +copy.setTime(birthday.getTime()); +</pre> + +<h3 id="测量代码执行时间">测量代码执行时间</h3> + +<p>连续调用两个新生成的日期对象的 getTime 方法,根据两次调用的返回值求得时间差。这可以用于计算某些操作的执行时间。避免生成不必要的{{jsxref("Date")}}对象另见{{jsxref("Date.now()")}} </p> + +<pre class="brush:js">var end, start, i; + +start = new Date(); +for (i = 0; i < 1000; i++) { + Math.sqrt(i); +} +end = new Date(); + +console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");</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>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.9.5.9', 'Date.prototype.getTime')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-date.prototype.gettime', 'Date.prototype.getTime')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Date.getTime")}}</p> + +<h2 id="See_Also" name="See_Also">相关链接</h2> + +<ul> + <li>{{jsxref("Date.prototype.setTime()")}}</li> + <li>{{jsxref("Date.prototype.valueOf()")}}</li> + <li>{{jsxref("Date.now()")}}</li> +</ul> |