JavaScript Now 函数是用于返回当前日期和时间的毫秒数的 Date 函数之一。本文将通过示例向您展示如何使用 Date.Now。
以下示例可帮助您了解 now 函数。
<!DOCTYPE html>
<html>
<head>
<title> JS </title>
</head>
<body>
<h1> Example </h1>
<script>
var dt = Date.now();
document.write("Date and Time in Milliseconds : " + dt);
</script>
</body>
</html>
如您所见,它返回的是当前的日期和时间的毫秒数。
Example
Date and Time in Milliseconds: 1541383869864
在本例中,我们将它返回的毫秒数转换为正常的日期和时间。对于此 JavaScript 示例,您需要使用 Js 函数。
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Now Function </title>
</head>
<body>
<h1> Example </h1>
<script>
var dt = Date(Date.now());
document.write("DateTime : " + dt);
</script>
</body>
</html>
Example
DateTime : Mon Nov 05 2018 07:39:09 GMT+0530 (Indian Standard Time)
JavaScript Date now 函数 示例 2
以下示例可帮助您了解 now() 和 Date() 函数。正如我们前面所说,这两个函数返回的结果相同。
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptNowFunction </title>
</head>
<body>
<h1> JavaScriptDateNowFunction Example </h1>
<script>
var dt1 = Date(Date.now());
document.write("Date and Time : " + dt1 + "<br/>");
document.write("Date and Time Example 2 <br/>");
var dt2 = Date();
document.write("Date and Time : " + dt2);
</script>
</body>
</html>
