Home > Article > Web Front-end > How to convert js timestamp to date format
How to convert js timestamp to date format: First, you can get the timestamp through the getTime method; and then convert it into the corresponding date format through the Date toLocaleString method.
【Recommended course: JavaScript Tutorial】
js method to get the current timestamp:
Method 1:
<script type="text/javascript"> var timestamp1 = Date.parse(new Date()); document.write(timestamp1); </script>
This method will get the timestamp in milliseconds Change to 000 to display
Method 2:
<script type="text/javascript"> var date = new Date('2019-03-01 13:22:49:123'); var time1 = date.getTime(); document.write(time1); </script>
This method is to get the timestamp of the current millisecond
As shown in the picture Show
js to convert the timestamp into a common date format
Method 1: Date toLocaleString method
<script type="text/javascript"> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); } document.write(getLocalTime(1551417769)); </script>
Rendering:
##Method 2:
<script type="text/javascript"> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); } document.write(getLocalTime(1551417769)); </script>
Rendering:
##Summary: The above is the entire content of this article, I hope it will be helpful to everyone
The above is the detailed content of How to convert js timestamp to date format. For more information, please follow other related articles on the PHP Chinese website!