如何在 JavaScript 中获取时间戳?
在本教程中,我们将学习如何在 JavaScript 中获取时间戳。软件开发人员或应用程序开发人员从来不会不需要使用日期和时间。在许多情况下,他们需要使用日期和时间戳。例如,要在用户的仪表板上显示时间,请将操作的时间保存在数据库中。
现在,让我们了解时间戳是什么?时间戳是从 1970 年 1 月 1 日 UNIX 纪元开始的总毫秒数。使用时间戳,我们可以提取日期、日、年、月、小时、分钟、秒、毫秒等。
所以,现在用户可以理解为什么时间戳很重要。与其存储整个日期和时间字符串,不如存储时间戳,这样我们可以在需要时提取完美的日期和时间。因此,它节省了数据库的空间。
在这里,我们有不同的方法来获取时间戳,使用 vanilla JavaScript 的内置库方法和 JavaScript 的 Moment JS 库。
使用 Date.now() 或 getTime() 方法
在 vanilla JavaScript 中,我们有一个内置的 Date 类来获取 date 和 time。用户可以直接使用 Date 类的 Date.now() 方法,该方法返回从 UNIX 纪元开始的总毫秒数。此外,以同样的方式,用户可以创建 Date 类的对象,并对该对象使用 getTime() 方法。
语法
以下是使用 Date.now() 方法和 getTime() 方法与 Date 类的对象。
- 使用 Date 类的 now() 方法。
let timestamp = Date.now();
- 使用 getTime() 方法。
let date = new Date(); // object of the date class timestamp = date.getTime(); // To get the timestamp
示例
在下面的示例中,我们只是使用 Date.now() 方法获取时间戳。此外,我们创建了 Date 类的对象,并使用该对象应用了调用的 getTime() 方法。
<!DOCTYPE html> <html> <body> <h2> Get a timestamp in JavaScript. </h2> <h4> Get timestamp using the Date.now() </h4> <div id="timestamp1"> </div> <h4> Get timestamp using the getTime() method </h4> <div id="timestamp2"> </div> <script> let timestamp1 = document.getElementById("timestamp1"); let timestamp2 = document.getElementById("timestamp2"); let timestamp = Date.now(); timestamp1.innerHTML = timestamp; let date = new Date(); // creating the object of the date class timestamp = date.getTime(); timestamp2.innerHTML = timestamp; </script> </body> </html>
使用 Moment JS valueof() 方法
Moment JS 是一个 JavaScript 库,具有处理日期和时间的超酷功能。在这里,我们将使用 valueof() 方法获取 UNIX 纪元启动以来的总毫秒数,而要获取总秒数,我们将使用 unix() 方法。
在使用这种方法之前,我们需要确保已在应用程序中安装了 Moment Js 库,或者正在使用 CDN 将 Moment JS 嵌入到我们的代码中。
语法
let timestamp = moment().valueof(); // timestamp in milliseconds. let timestamp = moment().unix(); // timestamp in seconds.
示例
以下示例演示了如何使用 Moment JS 库的 valueof() 方法。我们使用 valueof() 方法获取当前时间戳(以毫秒为单位)。此外,我们还实现了 Moment JS 的 unix() 方法来获取时间戳(以毫秒为单位)。
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js" integrity="sha512- vFABRuf5oGUaztndx4KoAEUVQnOvAIFs59y4tO0DILGWhQiFnFHiR+ZJfxLDyJlXgeut9Z07Svuvm+1Jv89w5g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> </head> <body> <h2> Get a timestamp in JavaScript. </h2> <h4> Get timestamp in milliseconds using the valueOf() method of Moment JS. </h4> <div id="timestamp1"> </div> <h4> Get timestamp in seconds using the valueOf() method of Moment JS.</h4> <div id="timestamp2"> </div> <script> let timestamp1 = document.getElementById("timestamp1"); let timestamp2 = document.getElementById("timestamp2"); let timestamp = moment().valueOf(); // timestamp in milliseconds. timestamp1.innerHTML = timestamp; timestamp = moment().unix(); // timestamp in seconds timestamp2.innerHTML = timestamp; </script> </body> </html>
时间戳的使用
JavaScript 中时间戳有多种用途,但这里我们通过示例展示了最佳用法。我们可以使用时间戳来查找两个日期之间的差值(以年、月、日或任何您想要的单位表示)。
语法
用户可以按照以下语法来查找两个时间戳之间的差值。
let startDate = new Date("01/23/1990"); // creating date object let diff = Date.now() - startDate.getTime(); // difference between current date and start date
示例
在下面的例子中,我们使用时间戳找出两个日期之间的年数、天数和周数。
<html> <body> <h2> Get a timestamp in JavaScript. </h2> <h4>Number of years between the 01 / 23 / 1990 and current date. </h4> <div id="timestamp1"> </div> <h4>Difference in weeks</h4> <div id="timestamp2"> </div> <h4> Difference in days</h4> <div id="timestamp3"> </div> <script> let timestamp1 = document.getElementById("timestamp1"); let timestamp2 = document.getElementById("timestamp2"); let timestamp3 = document.getElementById("timestamp3"); let startDate = new Date("01/23/1990"); let diff = Date.now() - startDate.getTime(); let day = 1000 * 60 * 60 * 24; // milliseconds of 1 day timestamp3.innerHTML = Math.floor(diff / day); let week = 7 * day // 7 days is equal to one week. timestamp2.innerHTML = Math.floor(diff / week); let year = 365 * day; // year has 365 days timestamp1.innerHTML = Math.floor(diff / year); </script> </body> </html>
结论
我们已经了解了如何使用 Moment JS 库方法和 JavaScript 的 Date() 类获取时间戳。此外,我们还通过一个包含有效代码的示例了解了时间戳的用法。