迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中获取时间戳

作者:迹忆客 最近更新:2024/03/17 浏览次数:

你可以使用 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() 函数稍快,但它们可能不适用于长数。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便