Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何写一个日期转换管道 #82

Open
deepthan opened this issue Aug 13, 2020 · 0 comments
Open

如何写一个日期转换管道 #82

deepthan opened this issue Aug 13, 2020 · 0 comments

Comments

@deepthan
Copy link
Owner

如何写一个日期转换管道

管道作用: 把 1509601513000 格式的时间转换为 2017-10-31 格式

  • share文件夹里面有个 date-transform.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name:'dateTransformPipe'})

export class DateTransformPipe implements PipeTransform {
    transform(val) {
        let date = new Date(val)
        let dateWanted = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        return  dateWanted;
    }
}

  • share.module.ts里面声明这个管道并抛出
import { DateTransformPipe } from './pipe/date-transform.pipe';
@NgModule({
  declarations: [ 
    DateTransformPipe
  ],
  exports: [ 
    DateTransformPipe
  ]
})

  • 在需要使用的位置所在模块引入share.module
import { ShareModule } from '../share/share.module';
@NgModule({
  imports: [
    ShareModule
  ]
})
  • 在需要用的html里面直接使用(license.component.ts)
  <div> {{ data.startDate | dateTransformPipe}} </div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant