Reputation: 253
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?
Upvotes: 3
Views: 12103
Reputation: 110
Week of Year and Week of Month are supported in DatePipe in Angular 7:
https://angular.io/api/common/DatePipe
Upvotes: 3
Reputation: 641
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()".
Upvotes: 3
Reputation: 17973
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.
Upvotes: 9
Reputation: 657546
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.
Upvotes: 1