📜  在javascript中打印名称时间(1)

📅  最后修改于: 2023-12-03 15:23:23.662000             🧑  作者: Mango

在 Javascript 中打印名称和时间

在 Javascript 中,要打印名称和时间可以使用 console.log() 函数和 Date() 对象。

打印名称

要打印名称,可以使用字符串字面量或变量作为参数传递给 console.log() 函数。以下是示例代码:

const name = "John Doe";
console.log(`My name is ${name}.`);

这将在控制台中打印出 "My name is John Doe."。

打印时间

要打印当前日期和时间,可以使用 Date() 对象和一些内置方法。以下是示例代码:

const now = new Date();
console.log(`The current time is ${now.toLocaleString()}.`);

这将在控制台中打印出当前的日期和时间,例如 "The current time is 6/15/2022, 2:30:55 PM"。

打印名称和时间

要同时打印名称和时间,可以组合字符串字面量或变量和 Date() 对象。以下是示例代码:

const name = "John Doe";
const now = new Date();
console.log(`My name is ${name} and the current time is ${now.toLocaleString()}.`);

这将在控制台中打印出 "My name is John Doe and the current time is 6/15/2022, 2:30:55 PM"。

以上就是在 Javascript 中打印名称和时间的方法。