js获取前十二个月第一天零点的时间戳
时间: 2023-06-28 13:04:14 浏览: 120
可以使用 JavaScript 中的 Date 对象来获取前十二个月第一天零点的时间戳。具体步骤如下:
1. 获取当前时间的年份和月份:
```
const today = new Date();
const thisYear = today.getFullYear();
const thisMonth = today.getMonth() + 1; // 月份从 0 开始,所以要加 1
```
2. 计算前十二个月的年份和月份:
```
const lastYear = thisMonth > 12 ? thisYear - 1 : thisYear;
const lastMonth = thisMonth > 12 ? thisMonth - 12 : thisMonth;
```
3. 获取前十二个月第一天的日期对象:
```
const firstDayOfLastMonth = new Date(lastYear, lastMonth - 1, 1);
```
4. 将日期对象转换为时间戳:
```
const timestamp = firstDayOfLastMonth.getTime();
```
完整代码如下:
```
const today = new Date();
const thisYear = today.getFullYear();
const thisMonth = today.getMonth() + 1;
const lastYear = thisMonth > 12 ? thisYear - 1 : thisYear;
const lastMonth = thisMonth > 12 ? thisMonth - 12 : thisMonth;
const firstDayOfLastMonth = new Date(lastYear, lastMonth - 1, 1);
const timestamp = firstDayOfLastMonth.getTime();
console.log(timestamp);
```
阅读全文
相关推荐















