如何在JavaScript中獲取當前日期和時間?
本教程將指導我們如何在JavaScript中獲取日期和時間。如今,JavaScript是用於使用者與瀏覽器互動的最流行的程式語言。可以說,幾乎找不到一個不使用JavaScript的Web應用程式。
建立本教程的目的是使程式設計師熟悉Date()物件。使用者可以使用Date 類獲取今天的日期和當前時間。在開發應用程式時,程式設計師可能需要根據使用者的本地時區顯示當前日期和時間,我們可以使用Date類來滿足此需求。此外,有時我們需要將使用者的會話開始和結束時間儲存在我們的應用程式資料庫中。
為了解決所有這些問題,在本教程中,我們只需建立一個Date類的物件,並使用其各種方法來獲取當前日期和時間。
使用Date物件獲取日期和時間
使用toLocaleString()方法獲取日期和時間
使用Date物件獲取日期和時間
Date 類包含幾種方法;我們可以使用它們來獲取當前的日期、星期幾和時間。此外,它還包含數百種不同的方法,程式設計師可以根據自己的需求使用它們。
語法
使用者可以按照以下語法使用不同的方法:
let newDate = new Date(); // create the new object of the date class
使用以下語法,使用者可以獲取年份、月份和日期。
let year = newDate.getFullYear(); // returns the current year let month = newDate.getMonth(); // returns month between 0 to 11. Users need to add +1 to the returned value to get current month. let todaySDate = newDate.getDate() // returns today’s date.
使用以下語法,使用者可以獲取小時、分鐘和秒。
let hours = newDate.getHours(); //returns the current hours of the day let minutes = newDate().getMinutes(); // returns the current minutes of hours let seconds = newDate.getSeconds(); // returns current the seconds of minutes
示例
以下示例說明如何使用上述方法從Date 物件獲取日期和時間。此外,我們還在月份的返回值中加了1,因為它返回的值介於0到11之間。
<html>
<head>
<title>Get date and time in JavaScript.</title>
</head>
<body>
<h2>Get date and time using JavaScript.</h2>
<h4>Output after using the basic methods of the Date() class.</h4>
<div id="date"> </div>
<div id="time"> </div>
<script type="text/javascript">
let date = document.getElementById("date");
let time = document.getElementById("time");
// creating the date object and getting the date and time
let newDate = new Date();
let year = newDate.getFullYear();
let month = newDate.getMonth();
let todaySDate = newDate.getDate();
let hours = newDate.getHours();
let minutes = newDate.getMinutes();
let seconds = newDate.getSeconds();
date.innerHTML = " " + todaySDate + "/ " + month + "/ " + year;
time.innerHTML = hours + ": " + minutes + ": " + seconds;
</script>
</body>
</html>在上面的輸出中,使用者可以看到我們根據本地時區獲取當前日期和時間。
使用toLocaleString()方法獲取日期和時間
在這種方法中,我們將像上述方法一樣使用Date物件,但是這裡我們將使用不同的高階方法。我們將使用toLocaleString()方法。它有兩個引數,一個是區域設定,另一個是選項。區域設定表示您要獲取資料的本地區域或地區,選項是一個包含許多屬性的物件。
語法
使用者可以按照以下語法使用高階Date物件方法:
let date = new Date(); let dateAndTime = date.toLocaleString(); // returns the date and time both. let date = date.toLocaleDateString(); // returns the date only. let time = date.toLocaleTimeString(); // returns the time only.
如果使用者想要獲取任何本地區域的日期和時間,可以按照以下語法:
let options = {
year: ‘numeric’,
month: ‘long’,
day: ‘numeric’,
weekday: ‘long’,
}
let date = date.toLocaleString( locale, options);引數
locale − locale引數表示本地區域的Unicode。如果您想獲取印度的當前日期和時間,則應傳入“en-IN”,對於美國使用者,可以傳入“en-US”。
options − options可以包含許多屬性來格式化日期和時間。例如,在上面的語法中,我們編寫了返回年份和日期的數字格式,並返回長格式的星期幾和月份。
示例
以下示例演示如何使用toLocaleString()方法獲取任何特定區域的日期和時間。
<html>
<head>
<title>Get date and time in JavaScript.</title>
</head>
<body>
<h4>Output after using toLocaleString() Method.</h4>
<div id="dateAndTime"> </div>
<h4> Output after using toLocaleDateString() Method.</h4>
<div id="date"> </div>
<h4> Output after using toLocaleTimeString() Method. </h4>
<div id="time"> </div>
<h4> Output after using toLocaleString() Method with locale 'en-IN'. </h4>
<div id="dateIN"> </div>
<h4> Output after using toLocaleString() Method with local 'en-US'. </h4>
<div id="dateUs"> </div>
<script type="text/javascript">
let dateAndTime = document.getElementById("dateAndTime");
let date = document.getElementById("date");
let time = document.getElementById("time");
let dateIN = document.getElementById("dateIN");
let dateUs = document.getElementById("dateUs");
// creating the date object and using the toLocaleString() method
let newDate = new Date();
dateAndTime.innerHTML = newDate.toLocaleString();
date.innerHTML = newDate.toLocaleDateString();
time.innerHTML = newDate.toLocaleTimeString();
// options for the toLocaleString() method
var options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long', };
dateIN.innerHTML = newDate.toLocaleString("en-IN", options);
dateUs.innerHTML = newDate.toLocaleString("en-US", options);
</script>
</body>
</html>在最後兩個輸出中,使用者可以看到我們根據區域獲得了不同的日期格式。
結論
Date類包含許多高階方法來獲取日期和時間。但是,使用者可以使用內建方法獲取完整的日期和時間字串。
使用者可以使用Date類的各種方法根據自己的需求獲取日期和時間資訊。此外,toLocaleString()方法允許您獲取區域日期和時間。此外,它還允許您透過將options物件作為引數傳遞來根據您的需要格式化日期和時間。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP