How does js convert timestamp to date

Timestamps can be converted to dates using methods on the Date object. The specific implementation is as follows:

```javascript
const timestamp = 1615497700000; // The timestamp to be converted
const date = new Date(timestamp); // Create a Date object based on the timestamp
const year = date.getFullYear(); // Get the year
const month = date .getMonth() + 1; // Get the month, need to add 1
const day = date.getDate(); // Get the date
const hour = date.getHours(); // Get the hour
const minute = date.getMinutes(); // Get minutes
const second = date.getSeconds(); // Get seconds

const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`; // concatenated into a formatted date string

console.log(formattedDate); // Output: 2021-3-11 15:35:0
```

In the above code, we use the getFullYear, getMonth, getDate, getHours, getMinutes and getSeconds methods in the Date object to get the year, month, day, hour, minute and second, and splice them into a formatted date String, and finally output through console.log.

Guess you like

Origin blog.csdn.net/qq_27487739/article/details/130464927