Home > Web Front-end > JS Tutorial > body text

How to convert js timestamp to date format

清浅
Release: 2020-09-07 15:02:28
Original
43713 people have browsed it

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.

How to convert js timestamp to date format

【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>
Copy after login

This method will get the timestamp in milliseconds Change to 000 to display

Method 2:

<script type="text/javascript">
  var date = new Date(&#39;2019-03-01 13:22:49:123&#39;);
    var time1 = date.getTime();
    document.write(time1);
	</script>
Copy after login

This method is to get the timestamp of the current millisecond

As shown in the picture Show

How to convert js timestamp to date format

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}$/,&#39; &#39;);     
}     
document.write(getLocalTime(1551417769));     
</script>
Copy after login

Rendering:

How to convert js timestamp to date format

##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>
Copy after login

Rendering:

How to convert js timestamp to date format##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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template