Angular2如何使用管道改变年龄

Lea*_*Lea 0 pipe angular

我想创建一个可以将年份(1992年)改为年龄(26岁)(韩国时代)的管道

我想我应该加载当前年份(2017年),减去1992年并加上+1

但我不知道如何用管道来实现这一点.

我提前感谢您的帮助.

Ale*_*nov 9

大段引用

这是我使用moment.js的例子(它还显示了几年的月份,随意删除它):

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
    name: 'age'
})
export class AgePipe implements PipeTransform {

    transform(value: Date): string {
        let today = moment();
                let birthdate = moment(value);
                let years = today.diff(birthdate, 'years');
                let html:string = years + " yr ";

                html += today.subtract(years, 'years').diff(birthdate, 'months') + " mo";

        return html;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后只需使用此管道:{{person.birthday|age}}在您的HTML中,person.birthdayJavascript Date对象在哪里.