无法在 JS 中应用 toDateString() 到日期

Ade*_*emo 4 javascript datetime typescript

我遇到了一个我试图理解的问题。我只是想通过使用 toDateString() 将日期转换为对读者更友好的格式。但是,这样做时我收到“toDateString() 不是函数”错误。

我可以使用 toString() 做到这一点:

truncateDate() {
    if (this.employee && this.employee.dob)
    {
        let birthDate = this.employee.dob;
        console.log(birthDate); // 2011-06-12T05:00:00.000Z Prints to console
        console.log(birthDate.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不能做 toDateString():

truncateDate() {
    if (this.employee && this.employee.dob)
    {
        let birthDate = this.employee.dob;
        console.log(birthDate); // 2011-06-12T05:00:00.000Z Prints to console
        console.log(birthDate.toDateString());
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

sum*_*mar 9

将字符串转换为日期对象,然后您就可以使用该函数。这是 MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

this.employee={}
this.employee.dob='1/2/2018'
let birthDate = this.employee.dob;
console.log(birthDate);
console.log(new Date(birthDate).toDateString());

//
Run Code Online (Sandbox Code Playgroud)