new Date 时间戳为0
时间: 2023-12-23 18:10:49 浏览: 35
new Date(0) 创建的日期对象代表的是1970年1月1日 00:00:00 UTC 时间。这个时间被称为UNIX纪元(Unix Epoch),并且在计算机系统中通常用来表示时间戳。在JavaScript中,时间戳是以毫秒为单位的,因此new Date(0)表示的是1970年1月1日 00:00:00.000 UTC。
相关问题
new Date时间戳转时间
可以使用JavaScript中的Date对象来将时间戳转换为时间。代码如下:
```javascript
const timestamp = 1629323660000; // 时间戳,以毫秒为单位
const date = new Date(timestamp);
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
const hour = ("0" + date.getHours()).slice(-2);
const minute = ("0" + date.getMinutes()).slice(-2);
const second = ("0" + date.getSeconds()).slice(-2);
const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
console.log(formattedDate); // 输出格式化后的时间字符串,例如:2021-08-19 18:21:00
```
这段代码将时间戳转换为一个Date对象,然后使用Date对象的方法获取年、月、日、时、分、秒等信息,并将其格式化为字符串。
Java获取date时间戳
Java获取date时间戳的方法是可以使用getTime()方法将Date类型转换为long类型,再除以1000转换为秒。例如:
```
Date date = new Date();
long timestamp = date.getTime()/1000;
System.out.println("时间戳:"+timestamp);
```