JS 获取当天23:59:59的时间戳
时间: 2023-07-21 14:31:03 浏览: 73
可以使用 JavaScript 的 Date 对象和 getTime 方法来获取当天23:59:59的时间戳,代码如下:
```javascript
var today = new Date();
today.setHours(23, 59, 59, 999);
var timestamp = Math.floor(today.getTime() / 1000);
console.log(timestamp);
```
这里使用 setHours 方法将当前时间设置为当天的23:59:59,使用 getTime 方法获取时间戳,然后除以1000取整得到秒级时间戳。
相关问题
js 通过 年月日 怎么获取当天的 00:00:00 和 23:59:59
你可以使用 JavaScript 的 Date 对象来获取指定日期的 00:00:00 和 23:59:59。下面是一种实现方式:
```javascript
// 获取当前日期
var today = new Date();
// 设置时间为 00:00:00
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);
// 获取当天的 00:00:00
var startTime = today.getTime();
// 设置时间为 23:59:59
today.setHours(23);
today.setMinutes(59);
today.setSeconds(59);
today.setMilliseconds(999);
// 获取当天的 23:59:59
var endTime = today.getTime();
```
通过上述代码,`startTime` 将存储当天的 00:00:00 的时间戳,而 `endTime` 则存储当天的 23:59:59 的时间戳。你可以根据需要对日期进行更改,这段代码将适用于大多数情况。
vue怎么获取获取每天00:00-02:00的时间戳
在Vue中,可以使用JavaScript中的Date对象来获取每天 00:00-02:00 的时间戳。具体步骤如下:
1. 获取当前时间戳:`const now = Date.now()`
2. 获取当天的 00:00 时间戳:
```
const today = new Date()
today.setHours(0)
today.setMinutes(0)
today.setSeconds(0)
today.setMilliseconds(0)
const today_start = +today
```
3. 判断当前时间是否在 00:00-02:00 之间,如果是,则返回当天的 00:00 到 02:00 时间戳,如果不是,则返回昨天的 00:00 到 02:00 时间戳。
```
let start_timestamp, end_timestamp
if (now < today_start + 2 * 3600 * 1000) {
// 当前时间在 00:00-02:00 之间
start_timestamp = today_start - 24 * 3600 * 1000
end_timestamp = today_start
} else {
// 当前时间在 02:00-24:00 之间
start_timestamp = today_start
end_timestamp = today_start + 2 * 3600 * 1000
}
```
其中,`start_timestamp` 表示每天 00:00 的时间戳,`end_timestamp` 表示每天 02:00 的时间戳。
注意:以上代码中使用的时间均为本地时间,如果需要使用 UTC 时间,请使用 `Date.UTC()` 方法获取当前时间。
相关推荐
![](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)