用ts实现一个方法,获取最近七天的时间戳,要去返回一个list
时间: 2023-11-22 18:54:47 浏览: 57
以下是用 TypeScript 实现获取最近七天时间戳的方法:
```typescript
function getRecentSevenDaysTimestamps(): number[] {
const oneDayTimestamp = 24 * 60 * 60 * 1000; // 一天的毫秒数
const nowTimestamp = Date.now(); // 当前时间戳
const sevenDaysAgoTimestamp = nowTimestamp - 6 * oneDayTimestamp; // 7天前的时间戳
const timestamps = [];
for (let i = sevenDaysAgoTimestamp; i <= nowTimestamp; i += oneDayTimestamp) {
timestamps.push(i);
}
return timestamps;
}
```
这个方法首先计算出一天的毫秒数 `oneDayTimestamp`,然后获取当前时间戳 `nowTimestamp` 和 7 天前的时间戳 `sevenDaysAgoTimestamp`。接着使用一个循环,从 7 天前的时间戳到当前时间戳,每隔一天生成一个时间戳,并将其存入数组 `timestamps` 中。最后返回这个数组即可。
相关问题
java定义一个工具方法返回上一季度结束那天最后的时间戳
可以使用以下代码定义一个工具方法来返回上一季度结束那天最后的时间戳:
```java
public static long getLastQuarterEndDayLastTimestamp() {
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int quarterEndMonth = ((currentMonth - 1) / 3) * 3; // 上一季度结束月份
int quarterEndDay = 1; // 上一季度结束日期为1号
if (quarterEndMonth == 0) { // 如果上一季度结束月份为0,则表示上一季度结束时间为去年的10月1号
quarterEndMonth = 10;
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
}
calendar.set(Calendar.MONTH, quarterEndMonth - 1);
calendar.set(Calendar.DAY_OF_MONTH, quarterEndDay);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
long endOfDayTimestamp = calendar.getTimeInMillis() / 1000; // 当天最后一刻时间戳
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long startOfDayTimestamp = calendar.getTimeInMillis() / 1000; // 当天第一刻时间戳
return endOfDayTimestamp - startOfDayTimestamp; // 返回当天最后一刻时间戳
}
```
在上述代码中,我们仍然是使用了 `java.util.Calendar` 类来计算上一季度结束的时间。具体来说,我们首先获取当前时间的月份,然后计算出上一季度结束的月份。如果上一季度结束的月份为0,则表示上一季度结束时间为去年的10月1号。
接下来,我们使用 `Calendar` 对象设置上一季度结束的日期、时间,并将其转换为时间戳(单位为秒)。然后,我们将时间设置为当天的最后一刻时间,并将其转换为时间戳,再将时间设置为当天的第一刻时间,并将其转换为时间戳。最后,我们将当天最后一刻时间戳减去当天第一刻时间戳,即可得到上一季度结束那天最后的时间戳。
使用js获取最近一个月的时间戳
好的,可以使用以下代码获取最近一个月的时间戳:
```
var timestamp = Date.now() - (30 * 24 * 60 * 60 * 1000);
console.log(timestamp);
```
这段代码通过获取当前时间戳(`Date.now()`)并减去一个月的毫秒数得到最近一个月的时间戳。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)