如何在JavaScript中获取当前日期?


当前回答

您可以使用扩展Date对象的Date.js库,因此可以使用.today()方法。

其他回答

Date.prototype.toLocalFullDateStringYYYYMMDDHHMMSS = function () {
if (this != null && this != undefined) {
    let str = this.getFullYear();
    str += "-" + round(this.getMonth() + 1);
    str += "-" + round(this.getDate());
    str += "T";
    str += round(this.getHours());
    str += ":" + round(this.getMinutes());
    str += ":" + round(this.getSeconds());
    return str;
} else {
    return this;
}

function round(n){
    if(n < 10){
        return "0" + n;
    }
    else return n;
}};

我的方式

let dateString = new Date().toLocaleString().split(',').find(() => true);

Try

`${Date()}`.slice(4,15)

console.log(`${Date()}`.slice(4,15))

这里我们使用标准的JS功能:模板文本、转换为字符串的Date对象和切片。这可能是满足OP要求的最短解决方案(没有时间,只有日期)

这可能会帮助你

var date = new Date();
console.log(date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getFullYear());

这将以dd/MM/yyyy格式打印当前日期

如果您只想要一个没有时间信息的日期,请使用:

var today=新日期();today.setHours(0,0,0);document.write(今天);