js 获取今天 近7天', '近30天', '近三个月', '近半年', '近一年', '近两年', '近三年'
时间: 2025-08-29 21:19:37 AIGC 浏览: 12
在 JavaScript 中,可以通过 `Date` 对象来获取当前日期,并结合 `setDate()`、`setMonth()` 和 `setFullYear()` 等方法计算过去的时间段。以下是如何实现这些功能的详细方法:
### 获取当前日期
要获取当前日期并格式化为 `YYYY-MM-DD` 格式:
```javascript
function getCurrentDate() {
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, "0");
const day = String(today.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
```
### 获取近7天日期
获取从今天开始往前推7天的所有日期,并按时间顺序排列:
```javascript
function getLast7Days() {
const dates = [];
const today = new Date();
for (let i = 6; i >= 0; i--) {
const date = new Date();
date.setDate(today.getDate() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
dates.push(`${year}-${month}-${day}`);
}
return dates;
}
```
### 获取近30天日期
通过循环生成最近30天的日期列表:
```javascript
function getLast30Days() {
const dates = [];
const today = new Date();
for (let i = 29; i >= 0; i--) {
const date = new Date();
date.setDate(today.getDate() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
dates.push(`${year}-${month}-${day}`);
}
return dates;
}
```
### 获取近三个月日期
使用 `setMonth()` 方法向前推3个月:
```javascript
function getLast3Months() {
const dates = [];
const today = new Date();
for (let i = 2; i >= 0; i--) {
const date = new Date();
date.setMonth(today.getMonth() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
dates.push(`${year}-${month}`);
}
return dates;
}
```
### 获取近半年日期
通过 `setMonth()` 方法向前推6个月:
```javascript
function getLast6Months() {
const dates = [];
const today = new Date();
for (let i = 5; i >= 0; i--) {
const date = new Date();
date.setMonth(today.getMonth() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
dates.push(`${year}-${month}`);
}
return dates;
}
```
### 获取近一年日期
使用 `setFullYear()` 方法向前推1年:
```javascript
function getLastYear() {
const dates = [];
const today = new Date();
for (let i = 11; i >= 0; i--) {
const date = new Date();
date.setMonth(today.getMonth() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
dates.push(`${year}-${month}`);
}
return dates;
}
```
### 获取近两年日期
向前推24个月以获取近两年的月份数据:
```javascript
function getLast2Years() {
const dates = [];
const today = new Date();
for (let i = 23; i >= 0; i--) {
const date = new Date();
date.setMonth(today.getMonth() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
dates.push(`${year}-${month}`);
}
return dates;
}
```
### 获取近三年日期
向前推36个月以获取近三年的月份数据:
```javascript
function getLast3Years() {
const dates = [];
const today = new Date();
for (let i = 35; i >= 0; i--) {
const date = new Date();
date.setMonth(today.getMonth() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
dates.push(`${year}-${month}`);
}
return dates;
}
```
阅读全文