Analysis of JS method for obtaining current timestamp

  • 2021-08-09 06:45:07
  • OfStack

Method 1: (This method is only accurate to seconds)

var timestamp = Date.parse(new Date());

Results: 1280977330000

Method 2:

var timestamp = (new Date()).valueOf();

Results: 1280977330748

Method 3:

 var timestamp=new Date().getTime();

Results: 1280977330748

Type 1: The time stamp obtained is changed from milliseconds to 000, because this method is only accurate to seconds

The second and third are to get the timestamp of the current millisecond.

Add 1 problem encountered


var a=(new Date()).toLocaleDateString()// Get the current date 
  a =a.replace(/\//g,'-');// Replace 2017/05/03  For   2017-05-03
var nowdate= (new Date(a))/1000;// Change the current date into a timestamp 
var wdate=(new Date(v.wdate))/1000;// Turn database date into time 

How to convert js timestamp into date format


// No. 1 1 Species 
function getLocalTime(nS) {
  return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}
alert(getLocalTime(1293072805));
// The result is 2010 Year 12 Month 23 Day  10:53
// No. 1 2 Species 
function getLocalTime(nS) {
  return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)
}
alert(getLocalTime(1293072805));
// No. 1 3 Species   The format is: 2010-10-20 10:00:00
  function getLocalTime(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/ Year | Month /g, "-").replace(/ Day /g, " ");
  }
  alert(getLocalTime(1177824835));

Related articles: