typescript 打字稿中的日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22246009/
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
DateTimeFormat in TypeScript
提问by tengl
I want to display a time string in 24 hour format and thought it would be easy in TypeScript. But I can't use the Date.toLocaleTimeString() with options for some reason. Any idea why? They are defined in a separate interface definition.
我想以 24 小时格式显示时间字符串,并认为在 TypeScript 中会很容易。但由于某种原因,我不能将 Date.toLocaleTimeString() 与选项一起使用。知道为什么吗?它们在单独的接口定义中定义。
interface Date {
toLocaleString(locales?: string[], options?: Intl.DateTimeFormatOptions): string;
toLocaleString(locale?: string, options?: Intl.DateTimeFormatOptions): string;
}
The other option is to use Intl.DateTimeFormat but the constructors return a Collator?
另一种选择是使用 Intl.DateTimeFormat 但构造函数返回一个 Collator?
var DateTimeFormat: {
new (locales?: string[], options?: DateTimeFormatOptions): Collator;
new (locale?: string, options?: DateTimeFormatOptions): Collator;
(locales?: string[], options?: DateTimeFormatOptions): Collator;
Is it a copy paste bug in lib.d.ts (same for NumberFormat) or how am I supposed to use them?
它是 lib.d.ts 中的复制粘贴错误(与 NumberFormat 相同)还是我应该如何使用它们?
Hopefully it's an easy fix, I'm pretty new to TypeScript so I might have missed something.
希望这是一个简单的修复,我对 TypeScript 还很陌生,所以我可能错过了一些东西。
回答by Fenton
This should work...
这应该工作...
var displayDate = new Date().toLocaleDateString();
alert(displayDate);
But I suspect you are trying it on something else, for example:
但我怀疑您正在尝试其他东西,例如:
var displayDate = Date.now.toLocaleDateString(); // No!
alert(displayDate);