tengl
tengl

Reputation: 573

DateTimeFormat in TypeScript

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.

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?

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?

Hopefully it's an easy fix, I'm pretty new to TypeScript so I might have missed something.

Upvotes: 37

Views: 109106

Answers (1)

Fenton
Fenton

Reputation: 250812

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);

Upvotes: 43

Related Questions