设置datetime-local显示今明两天
时间: 2024-06-17 20:04:41 浏览: 14
您好!要设置datetime-local显示今明两天,可以使用JavaScript中的Date对象进行计算,具体步骤如下:
1. 创建一个Date对象,获取当前时间:
```javascript
let now = new Date();
```
2. 分别获取当前时间的年、月、日:
```javascript
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
```
3. 计算明天的日期:
```javascript
let tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000);
let tomorrowYear = tomorrow.getFullYear();
let tomorrowMonth = tomorrow.getMonth() + 1;
let tomorrowDay = tomorrow.getDate();
```
4. 计算后天的日期:
```javascript
let afterTomorrow = new Date(now.getTime() + 48 * 60 * 60 * 1000);
let afterTomorrowYear = afterTomorrow.getFullYear();
let afterTomorrowMonth = afterTomorrow.getMonth() + 1;
let afterTomorrowDay = afterTomorrow.getDate();
```
5. 将日期转化为datetime-local格式,然后设置到相应的input标签上:
```javascript
let todayStr = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}T00:00`;
let tomorrowStr = `${tomorrowYear}-${tomorrowMonth < 10 ? '0' + tomorrowMonth : tomorrowMonth}-${tomorrowDay < 10 ? '0' + tomorrowDay : tomorrowDay}T00:00`;
let afterTomorrowStr = `${afterTomorrowYear}-${afterTomorrowMonth < 10 ? '0' + afterTomorrowMonth : afterTomorrowMonth}-${afterTomorrowDay < 10 ? '0' + afterTomorrowDay : afterTomorrowDay}T00:00`;
document.getElementById('input_today').value = todayStr;
document.getElementById('input_tomorrow').value = tomorrowStr;
document.getElementById('input_after_tomorrow').value = afterTomorrowStr;
```
其中,`input_today`、`input_tomorrow`、`input_after_tomorrow`分别是三个input标签的id。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)