如何在 TypeScript 中获取当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55306893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current date and time in TypeScript
提问by fidgetyphi
Summary
概括
I'm creating my first extension for VSCode in TypeScript, and want to create a new information message to display current date/time.
我正在 TypeScript 中为 VSCode 创建我的第一个扩展,并且想要创建一个新的信息消息来显示当前日期/时间。
What I've tried
我试过的
I've tried looking for some data/time keyword in the list of vscode key bindings. I've also tried looking for a function to get system date/time on Google only to come across solutions explaining data/time syntax and how to display a specific date/time but nothing to get the CURRENT data/time. I've also looked for it in vscode API documentation.
我试过在 vscode 键绑定列表中寻找一些数据/时间关键字。我还尝试在 Google 上寻找一个函数来获取系统日期/时间,只是为了遇到解释数据/时间语法以及如何显示特定日期/时间但没有获取当前数据/时间的解决方案。我也在 vscode API 文档中寻找过它。
Code part
代码部分
According to my understanding the code should be in extension.ts
file in activate
section where I register the command to implement showInformationMessage
function call and send a string to it containing the date/time. So here is the code around the line where I store the current date/time:
根据我的理解,代码应该在我注册命令以实现函数调用的部分的extension.ts
文件中activate
,showInformationMessage
并向它发送一个包含日期/时间的字符串。所以这里是我存储当前日期/时间的那一行的代码:
let disposableShowTime = vscode.commands.registerCommand(
"extension.showTime",
() => {
// Display a message box to the user
let dateTime = "22 March, 2019 12:45PM"; // store current data/time instead
vscode.window.showInformationMessage("Current time: " + dateTime);
}
);
Desired output: [Current Date/Time]
所需的输出:[Current Date/Time]
Actual output: 22 March, 2019 12:45PM
实际输出:22 March, 2019 12:45PM
回答by jure
To retrieve the current system Date/Time in javaScript/TypeScript you need to create a new Date
object using parameterless constructor, like this:
要在 javaScript/TypeScript 中检索当前系统日期/时间,您需要Date
使用无参数构造函数创建一个新对象,如下所示:
let dateTime = new Date()