租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

js怎么显示当前日期和时间,js显示数据

发布时间:2023-08-04 10:53:48

js怎样显示当前日期和时间

使用JavaScript可以通过以下方法显示当前日期和时间:
1. 使用`Date`对象获得当前日期和时间:
```js
var currentDate = new Date();
console.log(currentDate);
```
上述代码将打印出当前日期和时间,例如:`Mon Jun 28 2021 15:30:00 GMT+0800 (China Standard Time)`。
2. 使用`toLocaleString()`方法获得本地化确当前日期和时间:
```js
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`.
3. 使用`toLocaleDateString()`和`toLocaleTimeString()`方法分别获得本地化确当前日期和时间:
```js
var currentDate = new Date();
console.log(currentDate.toLocaleDateString());
console.log(currentDate.toLocaleTimeString());
```
上述代码将分别打印出经过格式化确当前日期和时间。
注意:以上方法获得的日期和时间都是根据用户计算机的时区和本地化设置来肯定的。