【问题标题】:Angular2 datePipe returning formatted date but with an additional monthAngular2 datePipe 返回格式化的日期,但还有一个月
【发布时间】:2017-01-03 10:59:50
【问题描述】:

这是我的格式化函数:

public formatDate(date: Date): string {
    var datePipe = new DatePipe();

    if (date) {
        return datePipe.transform(date, 'yMd');
    } else {
        return "--";
    }
};

当我使用它时

   let dateFooBar = new Date(2016, 5, 10); //NOtice month 5!!!!
   console.log("Date : " + this.formatDate(dateFooBar)) //output: Date : 6/10/2016

【问题讨论】:

    标签: angular angular2-template angular2-directives angular2-services


    【解决方案1】:

    这是 Angular 2 JavaScript 中 Date 类的正常行为,看看这个:

    export class HeroBirthdayComponent {
        birthday = new Date(1988, 3, 15); // April 15, 1988
    }
    

    您可以查看我提供的官方文档和示例on Angular 2 official website。

    您还可以在w3schools 上查看 JavaScript Date 类以了解更多详细信息。

    【讨论】:

    • 这是 JavaScript 中 Date 类的正常行为,它按照惯例使用从零开始的月份(0 = 一月,1 = 二月等)。
    • @MichaelLiu 你完全正确,我编辑了我的答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多