js中2021.12.13只要2021
时间: 2024-05-15 21:19:31 浏览: 20
可以使用字符串的截取方法substring()或slice(),或者使用正则表达式来提取年份。
例如:
1. 使用substring()方法:
```javascript
var date = "2021.12.13";
var year = date.substring(0,4);
console.log(year); //输出2021
```
2. 使用slice()方法:
```javascript
var date = "2021.12.13";
var year = date.slice(0,4);
console.log(year); //输出2021
```
3. 使用正则表达式:
```javascript
var date = "2021.12.13";
var year = date.match(/\d{4}/)[0];
console.log(year); //输出2021
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)