javascript - 日期值不断返回 NaN
问题描述
我想在 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);
console.log 显示类似“这里的日期格式是 NaN”的输出
谁能说这里有什么问题?
解决方案
它只是:
console.log('the date format here is ', date);
不需要'+'
如果您正在考虑使用加号运算符使用字符串连接+
,则正确的语法是
console.log('the date format here is ' + date);
但是,当涉及到您面临的场景时,我个人更喜欢 ES6 的模板文字。
console.log(`the date format here is ${date}`);
推荐阅读
- apache - Flask 中没有名为 numpy 的模块
- javascript - 尝试使用 jQuery 更改 Wordpress 粘性徽标图像 src
- xslt - XSLT:为输出文件中的每个元素创建一行
- java - 无法解析的构建扩展:插件 org.apache.felix:maven-bundle-plugin:2.5.0 或其依赖项之一无法解析
- python - 用等效的 UTF-8 格式“o”替换“ó”等非 ASCII 字符
- javascript - 实现轮播未能禁用可拖动属性
- sql - Oracle 联接语法中具有多个条件的外部联接
- python - 如何使用 Selenium 和 Python 点击雅虎页面上的“邮件”按钮
- html - 无法使用 Materialize css 提交表单
- postgresql - 权限检查postgresql函数