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


当前回答

这一个有一个解决方案:在js中将unixtime stamp转换为tim

var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();

其他回答

我必须创建一个TIMESTAMP,尽管我的DB映射上的类型是String,为此我使用了

new Date().toISOString();

输出类似于“2023-01-09T14:11:31.931Z”

var timestamp = Number(new Date()); // current time as number
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())));
}
time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);

在JavaScript中获取时间戳

在JavaScript中,时间戳是自1970年1月1日以来经过的毫秒数。如果您不打算支持<IE8,可以使用

new Date().getTime(); + new Date(); and Date.now();

直接获取时间戳,而无需创建新的Date对象。

返回所需的时间戳

new Date("11/01/2018").getTime()