“Error as ‘`’ (backquotes) not allowed in JavaScript (ECMA5)”

huangapple go评论68阅读模式
英文:

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+&#39;-&#39;+month+&#39;-&#39;+day+&#39; &#39;+hours+&#39;:&#39;+minutes+&#39;:&#39;+seconds;

huangapple
  • 本文由 发表于 2023年6月8日 23:21:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定