3

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?

Sujatha Girijala
  • 1,141
  • 8
  • 20
JanVanHaudt
  • 253
  • 1
  • 6
  • 16

4 Answers4

9

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.

neox5
  • 1,621
  • 13
  • 16
Patrick
  • 17,669
  • 6
  • 70
  • 85
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()".

Kenny
  • 571
  • 5
  • 18
3

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

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

Sisyphus40
  • 110
  • 4
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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567