angular 2 date pipe weeknumber

匆匆过客 提交于 2019-12-12 13:39:27

问题


I looked up how to return the week number in Angular 2. I have not found an answer to this question. I did find on https://docs.angularjs.org/api/ng/filter/date that in Angular 1 it would be something like this: {{today | date:'w'}} but this does not seem to work in Angular 2. I know I can write a function to take care of this but this doesn't seem practical. Am I missing something in the documentation about Angular 2 or is this not (yet) implemented there?


回答1:


The DatePipe currently doesn't support weekOfYear.

You can implement your own WeekOfYear pipe though.
See https://angular.io/docs/ts/latest/guide/pipes.html for more details.




回答2:


As Günter suggests, writing your own is pretty simple.

Create a new typescript file, week.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'week' })
export class WeekPipe implements PipeTransform {
    transform(value: Date): number {
        return this.getWeekNumber(value);
    }

    // source: http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
    private getWeekNumber(d: Date): number {
        // Copy date so don't modify original
        d = new Date(+d);
        d.setHours(0, 0, 0);
        // Set to nearest Thursday: current date + 4 - current day number
        // Make Sunday's day number 7
        d.setDate(d.getDate() + 4 - (d.getDay() || 7));
        // Get first day of year
        var yearStart = new Date(d.getFullYear(), 0, 1);
        // Calculate full weeks to nearest Thursday
        var weekNo = Math.ceil((((d.valueOf() - yearStart.valueOf()) / 86400000) + 1) / 7);
        // Return array of year and week number
        return weekNo;
    }
}

If you are using moment the code is even easier

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';

@Pipe({ name: 'week' })
export class WeekPipe implements PipeTransform {
    transform(value: Date): number {
        return moment(value).week();
    }
}

include the pipe in your app.module

import { NgModule } from '@angular/core';
import { WeekPipe } from './pipes/week.pipe';
@NgModule({
    imports: [
        // your imports
    ],
    declarations: [
        AppComponent,
        WeekPipe      // including the pipe in declarations
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

And then you can use it in your HTML as usual

<div class="week-number">
    {{ yourDate | week }}
</div>

where yourDate is a public yourDate: Date = new Date(); in your component.




回答3:


Edit: Typo I mentioned right here has been fixed.

I don't have the reputation to comment on Patrick's example, but I want to state that there is a typo:

import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';

@Pipe({ name: 'week' })
export class WeekPipe implements PipeTransform {
    transform(value: Date): number {
        return moment(value).week();
    }
}

Mark the "value" parameter that is passed to "moment()".




回答4:


Week of Year and Week of Month are supported in DatePipe in Angular 7:

https://angular.io/api/common/DatePipe



来源:https://stackoverflow.com/questions/35474506/angular-2-date-pipe-weeknumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!