JavaScript中的Date.now()函数
Date对象是JavaScript语言中内置的数据类型。日期对象使用新的Date()创建,如下所示。
创建Date对象后,可以使用多种方法对其进行操作。大多数方法仅允许您使用本地时间或UTC(通用或GMT)时间来获取和设置对象的年,月,日,时,分,秒和毫秒字段。
now()Date对象的函数返回自1970年1月1日以来的毫秒数。
语法
其语法如下
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日开始的毫秒数nu8mber来获取格式化的日期,并将获得的值传递给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)
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语