Convert the timestamp to the js code in date format

  • 2020-05-19 04:31:24
  • OfStack


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

As a result,
December 23, 2010 at 10:53
2 kinds of

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

What if you want something in this format?
2010-10-20 10:00:00
Take a look at the code below

<script>     
    function getLocalTime(nS) {     
       return new Date(parseInt(nS) * 1000).toLocaleString().replace(/ years | month /g, "-").replace(/ day /g, " ");      
    }     
    alert(getLocalTime(1177824835));     
    </script>

Or you could write it like this

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));

Ok, problem solving
Here's the thing to note
Do not pass Date in the string, but process 1 first, so you can handle it easily
You can use the replace method
As follows:

replace("/Date(","").replace(")/","");

Related articles: