在 JavaScript 中获取时间戳
你可以使用 JavaScript 中的 Date.now()
函数来获取时间戳。本教程演示了使用 Date.now()
函数的过程,你可以将其作为指南。
使用 JavaScript 中的 Date.now()
函数获取时间戳
我们可以使用 Date.now()
函数在 JavaScript 中以毫秒为单位获取时间戳。Date.now()
函数返回自 01-01-1970 以来经过的毫秒数。例如,让我们使用 JavaScript 中的 Date.now()
函数计算传递的毫秒数。请参考下面的代码。
var t = Date.now();
console.log(t);
输出:
1622872385158
输出显示自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的毫秒数。让我们将时间转换为秒和年,并使用 console.log()
函数将其显示在控制台上。请参考下面的代码。
var t = Date.now();
console.log(t);
var time = Date.now();
var timeInSeconds = Math.floor(time / 1000);
var timeInYears = Math.floor(timeInSeconds / (60 * 60 * 24 * 365));
console.log('Time Passed Since January 1, 1970 00:00:00 UTC');
console.log('Time In Seconds =', timeInSeconds, 's');
console.log('Time in Years = ', timeInYears, 'Years')
输出:
Time Passed Since January 1, 1970 00:00:00 UTC
Time In Seconds = 1622872385 s
Time in Years = 51 Years
正如你在输出中看到的,自 1970 年以来已经过去了 51 年;这意味着我们目前生活在 2021 年。同样,我们也可以使用转换公式找到当前的月份、日期和时间。Date.now()
函数通常用于查找程序或代码段运行所需的时间。你可以在代码的开始和结束处找到时间并评估时差。例如,让我们找出上面代码运行所花费的时间。请参考下面的代码。
var time = Date.now();
var timeInSeconds = Math.floor(time / 1000);
var timeInYears = Math.floor(timeInSeconds / (60 * 60 * 24 * 365));
console.log('Time Passed Since January 1, 1970 00:00:00 UTC');
console.log('Time In Seconds =', timeInSeconds, 's');
console.log('Time in Years = ', timeInYears, 'Years')
var newtime = new Date().getTime();
var timepassed = newtime - time;
console.log('Time Taken By this Code to Run =', timepassed, 'ms');
输出:
Time Passed Since January 1, 1970 00:00:00 UTC
Time In Seconds = 1622872385 s
Time in Years = 51 Years
Time Taken By this Code to Run = 1 ms
在输出中,这段代码所用的时间是 1 毫秒。在这里,你可以使用 Date.now()
函数来检查不同函数的性能。在上面的程序中,我们使用 Math.floor()
函数将浮点数转换为整数。你还可以使用按位运算符(如按位 NOT ~~
)将浮点数转换为整数。按位运算符比 Math.floor()
函数稍快,但它们可能不适用于长数。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。