日期值不断返回NaN

Obi*_*iha 2 javascript date

我想在Javascript中使用当前日期为'YYYY-MM-DD'格式的变量。但是当我执行我的代码并在console.log中检查它时。它只是说NaN

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
console.log("the date format here is ", + date);
Run Code Online (Sandbox Code Playgroud)

console.log显示的输出类似于“此处的日期格式为NaN”

谁能说这里出什么问题了?

wen*_*jun 6

只是:

console.log('the date format here is ', date);
Run Code Online (Sandbox Code Playgroud)

不需要'+'

如果您想使用加号运算符使用字符串连接+,则正确的语法为

console.log('the date format here is ' + date);
Run Code Online (Sandbox Code Playgroud)

但是,涉及到您面临的场景时,我个人会认为ES6的模板文字是。

console.log(`the date format here is ${date}`);
Run Code Online (Sandbox Code Playgroud)