Introduction of Several Methods of Formatting js Time Stamp into Date Format

  • 2021-07-21 07:30:07
  • OfStack

js needs to change the timestamp into a normal format, which may not be used in a normal situation.

Let's look at the first one first


function getLocalTime(nS) { 
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); 
} 
alert(getLocalTime(1293072805)); 

Type 2


function getLocalTime(nS) { 
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)} 
alert(getLocalTime(1293072805)); 

Regular substitution


function getLocalTime(nS) { 
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/ Year | Month /g, "-").replace(/ Day /g, " "); 
} 
alert(getLocalTime(1177824835)); 

Type 3


function formatDate(now) { 
var year=now.getYear(); 
var month=now.getMonth()+1; 
var date=now.getDate(); 
var hour=now.getHours(); 
var minute=now.getMinutes(); 
var second=now.getSeconds(); 
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
} 
var d=new Date(1230999938); 
alert(formatDate(d)); 

Related articles: