process.hrtime.bigint() 如何转化为 yyyy-MM-dd hh:mm:ss 格式的时间字符串?
时间: 2024-05-12 21:17:53 浏览: 18
可以使用以下代码将 process.hrtime.bigint() 转换为 yyyy-MM-dd hh:mm:ss 格式的时间字符串:
```
const hrTime = process.hrtime.bigint();
const seconds = Number(hrTime / 1000000000n);
const milliseconds = Number((hrTime % 1000000000n) / 1000000n);
const dateObj = new Date(seconds * 1000);
const year = dateObj.getUTCFullYear();
const month = (`0${dateObj.getUTCMonth()+1}`).slice(-2);
const day = (`0${dateObj.getUTCDate()}`).slice(-2);
const hour = (`0${dateObj.getUTCHours()}`).slice(-2);
const minute = (`0${dateObj.getUTCMinutes()}`).slice(-2);
const second = (`0${dateObj.getUTCSeconds()}`).slice(-2);
const dateString = `${year}-${month}-${day} ${hour}:${minute}:${second}.${milliseconds}`;
```
注意:此代码返回的是 UTC 时间,如果需要本地时间则需要根据所在时区进行修改。
相关推荐
![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)
![](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)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)