Skip to content

Commit 7b3d1d3

Browse files
authored
Merge pull request #224 from ioet/223-pipe-date-diff
fix: #224 date diff pipe
2 parents 996fecf + b134e9f commit 7b3d1d3

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { CreateProjectTypeComponent } from './modules/customer-management/compon
5656
import { CustomerEffects } from './modules/customer-management/store/customer-management.effects';
5757
import { EntryEffects } from './modules/time-clock/store/entry.effects';
5858
import { InjectTokenInterceptor } from './modules/shared/interceptors/inject.token.interceptor';
59+
import { SubstractDatePipe } from './modules/shared/pipes/substract-date/substract-date.pipe';
5960

6061
@NgModule({
6162
declarations: [
@@ -90,6 +91,7 @@ import { InjectTokenInterceptor } from './modules/shared/interceptors/inject.tok
9091
ProjectTypeListComponent,
9192
CreateProjectTypeComponent,
9293
EntryFieldsComponent,
94+
SubstractDatePipe,
9395
],
9496
imports: [
9597
CommonModule,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SubstractDatePipe } from './substract-date.pipe';
2+
3+
describe('SubstractDatePipe', () => {
4+
it('create an instance', () => {
5+
const pipe = new SubstractDatePipe();
6+
expect(pipe).toBeTruthy();
7+
});
8+
9+
it('returns the date diff', () => {
10+
const fromDate = new Date('2011-04-11T10:20:30Z');
11+
const substractDate = new Date('2011-04-11T08:00:30Z');
12+
13+
const diff = new SubstractDatePipe().transform(fromDate, substractDate);
14+
15+
expect(diff).toBe('02:20');
16+
});
17+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'substractDate'
5+
})
6+
export class SubstractDatePipe implements PipeTransform {
7+
8+
transform(fromDate: Date, substractDate: Date): string {
9+
const difference = fromDate.valueOf() - substractDate.valueOf();
10+
11+
const minutes = Math.floor((difference / (1000 * 60)) % 60);
12+
const hours = Math.floor(difference / (1000 * 60 * 60) % 24);
13+
14+
return `${this.formatTime(hours)}:${this.formatTime(minutes)}`;
15+
}
16+
17+
formatTime(time: number): string {
18+
const formattedTime = (time < 10) ? '0' + time : time.toString();
19+
return formattedTime;
20+
}
21+
22+
}

0 commit comments

Comments
 (0)