我想要一个表示当前日期和时间的数字,比如Unix时间戳。


当前回答

var time = Date.now || function() {
  return +new Date;
};

time();

其他回答

//当前Unix时间戳//自1970年1月1日起,1443534720秒。(UTC)//秒console.log(数学地板(newDate().valueOf()/1000));//1443534720console.log(数学地板(Date.now()/1000));//1443534720console.log(数学地板(newDate().getTime()/1000));//1443534720//毫秒console.log(数学地板(newDate().valueOf()));//1443534720087console.log(数学地板(Date.now()));//1443534720087console.log(数学地板(newDate().getTime()));//1443534720087//jQuery//秒console.log(数学地板($.now()/1000));//1443534720//毫秒console.log($.now());//1443534720087<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>

前几天,我从JQueryCookie的源代码中学习了一种将给定的Date对象转换为Unix时间戳的非常酷的方法。

下面是一个示例:

var date = new Date();
var timestamp = +date;

//如果你需要10位数alert('timestamp'+ts());函数ts(){return parseInt(Date.now()/1000);}

console.log(newDate().valueOf());//返回自epoch以来的毫秒数

我还没见过

Math.floor(Date.now() / 1000); // current time in seconds

另一个我还没看到的是

var _ = require('lodash'); // from here https://lodash.com/docs#now
_.now();