英文:
Error as '`' (backquotes) not allowed in javascript (ecma5)
问题
以下是您要翻译的代码部分:
var now = new Date();
var year = now.getUTCFullYear();
var month = now.getUTCMonth() + 1;
var day = now.getUTCDate();
var hours = now.getUTCHours();
var minutes = now.getUTCMinutes();
var seconds = now.getUTCSeconds();
var milliseconds = now.getUTCMilliseconds();
var test = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(test);
Are backquotes not allowed in javascript (`) ?
I am trying in ecma5 but getting error as mentioned above.
And also, can we match regular expression for above mentioned date-time format ?
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var now = new Date();
var year = now.getUTCFullYear();
var month = now.getUTCMonth() + 1;
var day = now.getUTCDate();
var hours = now.getUTCHours();
var minutes = now.getUTCMinutes();
var seconds = now.getUTCSeconds();
var milliseconds = now.getUTCMilliseconds();
var test = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(test);
<!-- end snippet -->
Are backquotes not allowed in javascript (`) ?
I am trying in ecma5 but getting error as mentioned above.
And also, can we match regular expression for above mentioned date-time format ?
答案1
得分: 1
目前,JS 的最新版本是 EcmaScript6,许多在线参考资料使用 EcmaScript13 规范。
在 Ecmascript 5 中不允许使用反引号(backticks),它们是在 EcmaScript6 中添加的,以更轻松地访问多行字符串。
您可以使用 + 号:
let a = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
英文:
Right now, the lastest version of JS is EcmaScript6, and many references online use EcmaScript13 specs.
Backticks are not allowed in Ecmascript 5, they were added to serve easier access to multi-lined strings in EcmaScript6.
You can use the + sign:
let a = year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论