Home  >  Article  >  Web Front-end  >  How to convert js timestamp to date format

How to convert js timestamp to date format

清浅
清浅Original
2019-03-01 13:49:0943721browse

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>

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>

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>

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>

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!

Statement:
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