溫馨提示×

js怎么顯示當前日期和時間

js
小億
536
2023-08-04 01:08:34
欄目: 編程語言

使用JavaScript可以通過以下方法顯示當前日期和時間:

  1. 使用Date對象獲取當前日期和時間:
var currentDate = new Date();
console.log(currentDate);

上述代碼將打印出當前日期和時間,例如:Mon Jun 28 2021 15:30:00 GMT+0800 (China Standard Time)。

  1. 使用toLocaleString()方法獲取本地化的當前日期和時間:
var currentDate = new Date();
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' };
console.log(currentDate.toLocaleString('en-US', options));

上述代碼將打印出經過格式化的當前日期和時間,例如:Monday, June 28, 2021, 15:30:00.

  1. 使用toLocaleDateString()toLocaleTimeString()方法分別獲取本地化的當前日期和時間:
var currentDate = new Date();
console.log(currentDate.toLocaleDateString());
console.log(currentDate.toLocaleTimeString());

上述代碼將分別打印出經過格式化的當前日期和時間。

注意:以上方法獲取的日期和時間都是根據用戶計算機的時區和本地化設置來確定的。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女