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


当前回答

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

其他回答

如果用于日志记录,则可以使用ISOString

new Date().toISOString()

“2019-05-18T20:02:36.694Z”

建议的正确方法是Number(new Date()),就代码可读性而言,

此外,UglifyJS和Google闭包编译器将降低已解析代码逻辑树的复杂性(如果您使用其中一个来隐藏/缩小代码)。

对于时间分辨率较低的Unix时间戳,只需将当前数字除以1000,保持整数。

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

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

new Date().toISOString();

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

/**
 * Equivalent to PHP's time(), which returns
 * current Unix timestamp.
 *
 * @param  {string}  unit    - Unit of time to return.
 *                           - Use 's' for seconds and 'ms' for milliseconds.
 * @return {number}
 */
time(unit = 's') {
    return unit == 's' ? Math.floor(Date.now() / 1000) : Date.now()
}