javascript+获取当前时间
时间: 2024-01-09 22:23:10 浏览: 31
以下是两种JavaScript获取当前时间的方法:
1. 使用Date对象获取当前时间
```javascript
var currentDate = new Date();
var year = currentDate.getFullYear();
var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
var day = ("0" + currentDate.getDate()).slice(-2);
var hour = ("0" + currentDate.getHours()).slice(-2);
var minute = ("0" + currentDate.getMinutes()).slice(-2);
var second = ("0" + currentDate.getSeconds()).slice(-2);
var currentTime = year + month + day + hour + minute + second;
console.log(currentTime); // 输出:当前时间的yyyymmddhhmmss格式
```
2. 使用document.lastModified属性获取当前网页最后修改时间
```javascript
var lastModified = document.lastModified;
console.log(lastModified); // 输出:当前网页最后修改时间
```
相关问题
JavaScript怎么获取当前时间
可以使用JavaScript内置的Date对象来获取当前时间。具体方法如下:
```javascript
var now = new Date(); // 创建一个Date对象,表示当前时间
var year = now.getFullYear(); // 获取当前年份
var month = now.getMonth() + 1; // 获取当前月份(注意月份从0开始,所以需要加1)
var day = now.getDate(); // 获取当前日期
var hour = now.getHours(); // 获取当前小时数
var minute = now.getMinutes(); // 获取当前分钟数
var second = now.getSeconds(); // 获取当前秒数
```
以上代码可以获取当前的年、月、日、时、分、秒,并将它们保存在对应的变量中。
javascript获取当前时间
### 回答1:
可以JavaScript的Date对象来获取当前时间。以下是获取当前时间的示例代码:
```javascript
var currentDate = new Date();
console.log(currentDate);
```
这将输出当前日期和时间的完整信息,例如:“Fri Dec 31 2021 17:18:34 GMT+0800 (中国标准时间)”
### 回答2:
在JavaScript中,可以使用内置的Date对象来获取当前的时间。
首先,我们需要创建一个Date对象:
```javascript
var currentDate = new Date();
```
现在,我们可以使用Date对象的方法来获取当前时间的各个部分。例如,要获取当前的小时数,可以使用getHours()方法:
```javascript
var currentHour = currentDate.getHours();
```
类似地,可以使用getMinutes()方法获取当前的分钟数,getSeconds()方法获取当前的秒数,getMilliseconds()方法获取当前的毫秒数。
如果我们只对当前的日期感兴趣,可以使用getDate()方法来获取当前的日期数,getMonth()方法获取当前的月份(注意:月份从0开始计数,即0表示一月,1表示二月,以此类推),getFullYear()方法获取当前的年份。
要将获取到的时间显示出来,可以将其存储到一个变量中,然后将其输出到控制台或网页上。例如:
```javascript
var currentTime = currentHour + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
console.log(currentTime);
```
以上代码将输出当前的小时数、分钟数和秒数。
总而言之,以上就是使用JavaScript获取当前时间的方法。我们可以根据自己的需求调用Date对象的不同方法来获取特定的时间部分,并将其输出到控制台或网页上进行显示。
### 回答3:
在JavaScript中,可以使用`Date`对象来获取当前时间。
```javascript
var currentDate = new Date(); // 创建一个Date对象
var currentYear = currentDate.getFullYear(); // 获取当前年份
var currentMonth = currentDate.getMonth() + 1; // 获取当前月份(注意月份从0开始,所以要+1)
var currentDay = currentDate.getDate(); // 获取当前日期
var currentHour = currentDate.getHours(); // 获取当前小时数
var currentMinute = currentDate.getMinutes(); // 获取当前分钟数
var currentSecond = currentDate.getSeconds(); // 获取当前秒数
console.log("当前时间:", currentYear, "年", currentMonth, "月", currentDay, "日", currentHour, "时", currentMinute, "分", currentSecond, "秒");
```
以上代码使用`Date`对象的各种方法来获取当前时间的年、月、日、小时、分钟和秒。在控制台使用`console.log()`方法打印输出当前时间。请注意,代码中的`getFullYear()`方法返回的是当前年份,而`getMonth()`方法返回的是从0开始的月份,需要自行加1。
如果你只想获取当前时间的字符串表示,可以使用`toLocaleString()`方法:
```javascript
var currentTimeString = currentDate.toLocaleString(); // 获取当前时间的字符串表示
console.log("当前时间:", currentTimeString);
```
以上代码中的`toLocaleString()`方法返回的是当前时间的字符串表示,包括日期和时间,格式可能因浏览器和操作系统而异。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)