我一直有一些麻烦,试图得到一个 Date对象在 TypeScript 格式化的方式,我希望它。
我有一个类 Module,它被定义为:
export class Module {
constructor(public id: number, public name: string, public description: string,
public lastUpdated: Date, public owner: string) { }
getNiceLastUpdatedTime(): String {
let options: Intl.DateTimeFormatOptions = {
day: "numeric", month: "numeric", year: "numeric",
hour: "2-digit", minute: "2-digit"
};
return this.lastUpdated.toLocaleDateString("en-GB", options) + " " + this.lastUpdated.toLocaleTimeString("en-GB", options);
}
}
当我使用以下代码调用该方法时:
let date = new Date(1478708162000); // 09/11/2016 16:16pm (GMT)
let module = new Module(1, "Test", "description", date, "test owner");
console.log(module.getNiceLastUpdatedTime());
最后,我在控制台上打印了以下内容:
'9 November 2016 16:16:02 GMT'
我想看到的是:
09/11/2015 16:16
我看了一下文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString,我仍然不知道我做错了什么(我知道这是一个 JavaScript API 文档,但是我很确定 TypeScript 正在暗中使用它)。