如何在 JavaScript 中獲取自 Unix 紀元以來的時間和日期?
在本教程中,我們將學習如何在 JavaScript 中獲取自 Unix 紀元以來的日期和時間。有時,程式設計師需要從**1970 年 1 月 1 日**(**Unix 紀元**開始)找到毫秒、秒、天或其他單位的總數。
因此,使用者可以使用 JavaScript 的**Date()** 類或 JavaScript 的 Moment.js 庫來獲取自紀元開始以來的日期和時間。
使用 Date() 類物件
在這種方法中,我們將學習如何從 1970 年 1 月 1 日開始的 Unix 紀元獲取當前日期。在 JavaScript 中,**Date()** 類包含**日期**和**時間**的方法和屬性。使用者可以建立 Date() 類的物件並在該物件上呼叫 Date() 類所有方法。
語法
使用者可以按照以下語法獲取自紀元以來的日期。
let date = new Date( ); // get date let totalMilliSeconds = date / 1; // get total milliseconds since epoch
示例
在下面的示例中,我們建立了 Date() 類的物件以獲取自紀元以來的日期。我們還透過將日期除以 1 來查詢自紀元以來的毫秒總數。使用者還可以獲取自紀元以來的秒數、分鐘數和小時數。
<html> <head> </head> <body> <h2> Get date and time since Unix epochs started in JavaScript. </h2> <h4> Get current Date using the <i> new Date() </i> object. </h4> <p id = "output1"> </p> <h4> Get total number of milliseconds from epochs using <i> new Date() </i> method.</h4> <p id = "output2"> </p> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let date = new Date(); output1.innerHTML += date; // divide date by 1 to get total milliseconds since epochs output2.innerHTML += date / 1; </script> </body> </html>
使用 Date.valueOf() 方法
在這種方法中,我們將使用日期類的**valueOf()** 方法。當用戶透過以日期類物件作為引用來呼叫**valueOf()** 方法時,它將返回自紀元以來的毫秒總數。我們可以從毫秒數獲取總小時數、天數等。
語法
使用者可以按照以下語法使用 valueOf() 方法並獲取自紀元開始以來的總小時數。
let milliseconds = new Date().valueOf(); // divide milliseconds by 1000 * 60 * 60 (seconds * minutes * hours) to get total hours let hours = Math.floor( milliseconds / (1000 * 60 * 60) );
示例
在下面的示例中,我們建立了日期類的物件,並使用*valueOf()* 方法找到了自紀元開始以來的毫秒總數。此外,我們還使用總毫秒數找到了自紀元以來的總小時數。
<html> <head> </head> <body> <h2> Get date and time since Unix epoch started in JavaScript. </h2> <h4> Get total number of milliseconds from epochs using <i> Date.valueOf() </i> method. </h4> <p id = "output1"> </p> <h4> Get total number of hours from epochs using <i> Date.valueOf() </i> method. </h4> <p id = "output2"> </p> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let milliseconds = new Date().valueOf(); output1.innerHTML += milliseconds; output2.innerHTML += Math.floor(milliseconds / (1000 * 60 * 60)); </script> </body> </html>
使用 Moment.js moment() 方法
在這種方法中,我們將使用 Moment.js 庫,這是一個用於操作日期的特殊庫。Moment.js 包含 moment() 方法,該方法返回毫秒總數。
使用者可以按照以下語法使用 Moment() 方法獲取自紀元以來的毫秒總數,並將其轉換為秒數和天數。
語法
let milliseconds = moment(); // dividing milliseconds by 1000 to get seconds let seconds = milliseconds / 1000; // dividing seconds with 60 * 60 *24 (seconds * minutes * hours) to get days let days = Math.floor( seconds / (60 * 60 * 24) );
示例
在下面的示例中,我們在*<head>* 標記中添加了 Moment.js CDN 以使用 moment() 方法。我們使用 moment() 方法建立了新的日期物件,該方法返回毫秒總數。此外,我們還將毫秒數轉換為秒數和天數。
<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 the date and time since the Unix epoch started in JavaScript.</h2> <h4>Get total number of seconds from epoch using <i>moment()</i> method.</h4> <p id = "output1"></p> <h4> Get total number of days from epochs using <i>moment()</i> method.</h4> <p id = "output2"></p> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let milliseconds = moment(); // get the seconds let seconds = milliseconds / 1000; output1.innerHTML += seconds; // get the total days since epoch output2.innerHTML += Math.floor(seconds / (60 * 60 * 24)); </script> </body> </html>
使用者已經學習瞭如何在 JavaScript 中獲取自紀元以來的時間和日期。使用者可以使用原生的 JavaScript Date() 類或 Moment.js 庫。最好使用 Moment.js,因為它包含比 Date() 類更多用於操作日期和時間的方法。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP