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


当前回答

function getTimeStamp() {
    var now = new Date();
    return ((now.getMonth() + 1) + '/' +
            (now.getDate()) + '/' +
             now.getFullYear() + " " +
             now.getHours() + ':' +
             ((now.getMinutes() < 10)
                 ? ("0" + now.getMinutes())
                 : (now.getMinutes())) + ':' +
             ((now.getSeconds() < 10)
                 ? ("0" + now.getSeconds())
                 : (now.getSeconds())));
}

其他回答

这似乎奏效了。

console.log(clock.now);
// returns 1444356078076

console.log(clock.format(clock.now));
//returns 10/8/2015 21:02:16

console.log(clock.format(clock.now + clock.add(10, 'minutes'))); 
//returns 10/8/2015 21:08:18

var clock = {
    now:Date.now(),
    add:function (qty, units) {
            switch(units.toLowerCase()) {
                case 'weeks'   :  val = qty * 1000 * 60 * 60 * 24 * 7;  break;
                case 'days'    :  val = qty * 1000 * 60 * 60 * 24;  break;
                case 'hours'   :  val = qty * 1000 * 60 * 60;  break;
                case 'minutes' :  val = qty * 1000 * 60;  break;
                case 'seconds' :  val = qty * 1000;  break;
                default       :  val = undefined;  break;
                }
            return val;
            },
    format:function (timestamp){
            var date = new Date(timestamp);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var hours = date.getHours();
            var minutes = "0" + date.getMinutes();
            var seconds = "0" + date.getSeconds();
            // Will display time in xx/xx/xxxx 00:00:00 format
            return formattedTime = month + '/' + 
                                day + '/' + 
                                year + ' ' + 
                                hours + ':' + 
                                minutes.substr(-2) + 
                                ':' + seconds.substr(-2);
            }
};

时间戳(毫秒)

要获取自Unix纪元以来的毫秒数,请调用Date.now:

Date.now()

或者,使用一元运算符+调用Date.prototype.valueOf:

+ new Date()

或者,直接调用valueOf:

new Date().valueOf()

要支持IE8和更早版本(请参阅兼容性表),请为Date.now创建一个垫片:

if (!Date.now) {
    Date.now = function() { return new Date().getTime(); }
}

或者,直接调用getTime:

new Date().getTime()

时间戳(秒)

要获取自Unix纪元以来的秒数,即Unix时间戳:

Math.floor(Date.now() / 1000)

或者,使用逐位或逐层稍快,但可读性也较低,将来可能会中断(参见解释1、2):

Date.now() / 1000 | 0

以毫秒为单位的时间戳(分辨率更高)

使用performance.now:

var isPerformanceSupported=(窗口.性能&&窗口.性能.当前&&窗口.性能.计时&&窗口.性能.计时.导航开始);var timeStampInMs=(是否支持性能?window.performance.now()+窗口.性能.计时.导航开始:日期.now());console.log(timeStampInMs,Date.now());

对于微秒分辨率的时间戳,有性能。现在:

function time() { 
  return performance.now() + performance.timing.navigationStart;
}

例如,这可能产生1436140826653.139,而Date.now仅产生143614086653。

Date.getTime()方法可以稍微调整一下:

getTime方法返回的值是毫秒数自1970年1月1日00:00:00 UTC开始。

将结果除以1000得到Unix时间戳,必要时为floor:

(new Date).getTime() / 1000

Date.valueOf()方法在功能上等同于Date.getTime(),这使得可以对Date对象使用算术运算符来获得相同的结果。在我看来,这种方法会影响可读性。

下面是另一个在JavaScript中生成时间戳的解决方案-包括单个数字的填充方法-在结果中使用天、月、年、小时、分钟和秒(jsfiddle的工作示例):

var pad = function(int) { return int < 10 ? 0 + int : int; };
var timestamp = new Date();

    timestamp.day = [
        pad(timestamp.getDate()),
        pad(timestamp.getMonth() + 1), // getMonth() returns 0 to 11.
        timestamp.getFullYear()
    ];

    timestamp.time = [
        pad(timestamp.getHours()),
        pad(timestamp.getMinutes()),
        pad(timestamp.getSeconds())
    ];

timestamp.now = parseInt(timestamp.day.join("") + timestamp.time.join(""));
alert(timestamp.now);