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


当前回答

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

new Date().toISOString();

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

其他回答

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

time();

要分别获得时间、月、日、年,这将起作用

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);
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())));
}

我喜欢这个,因为它很小:

+new Date

我也喜欢这个,因为它同样短,而且与现代浏览器兼容,超过500人投票认为它更好:

Date.now()