JavaScript 中的 Date.now() 函数
日期对象是 JavaScript 语言中内置的一种数据类型。使用 new Date( ) 如下所示创建日期对象。
一经创建 Date 对象,您可以使用许多方法进行操作。大多数方法只需让您获取并设置对象的年、月、日、小时、分钟、秒和毫秒字段,使用本地时间或 UTC(通用,或 GMT)时间。
Date 对象的 now() 函数返回自 1st Jan 1970 年起的毫秒数。
语法
其语法如下
Date.now();
示例
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var currentDate = Date.now();
var birthDate = Date.parse('29 sep 1989 00:4:00 GMT');
document.write(currentDate);
document.write(", "+birthDate+", ");
document.write(currentDate - birthDate);
</script>
</body>
</html>输出
1537780591654, 623030640000, 914749951654
示例
您可以使用此函数获取当前日期,但由于它返回从 1970 年 1 月 1 日起的毫秒数,所以要获取格式化的日期,需将获取的值传递给 Date 构造函数。
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var currentDate = Date.now();
document.write(currentDate);
document.write("<br>");
document.write(Date(currentDate).toString());
</script>
</body>
</html>输出
1539758885099 Wed Oct 17 2018 12:18:05 GMT+0530 (India Standard Time)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP