JavaScript getTime 函数

JavaScript getTime 函数是 Date 函数之一,它返回给定日期的毫秒数。此 getTime 函数返回自 1970 年 1 月 1 日到给定日期时间的总毫秒数。我们使用 getTime 函数来返回自默认日期到当前日期时间的毫秒数。

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt);
  document.write("Time : " + dt.getTime());
</script>
</body>
</html>
Example

Date and Time: Sun Nov 04 2018 15:48:43 GMT+0530 (Indian Standard Time)
Time : 1541326723757

在此示例中,我们在自定义日期上使用 JavaScript getTime 函数。

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptGetTimeFunction </title>
</head>
<body>
    <h1> JavaScriptgetTimeFunction Example </h1>
<script>
  var dt = Date("December 22, 2002 10:11:43");
  document.write("Date and Time : " + dt);
  document.write("Time using getTime(): " + dt.getTime());
</script>
</body>
</html>
getTime Function Example

在此示例中,我们在没有时间的情况下对自定义日期使用 getTime。此函数将时间视为 00:00:00,并返回自默认时间(1970 年 1 月 1 日)到指定日期的毫秒数。

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date("December 22, 1972");
  document.write("DateTime : " + dt);
  document.write("Time : " + dt.getTime());
</script>
</body>
</html>
Example

DateTime: Fri Dec 22 1972 00:00:00 GMT+0530 (Indian Standard Time)
Time : 93810600000