-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtilsService.ts
More file actions
42 lines (37 loc) · 1.13 KB
/
Copy pathDateUtilsService.ts
File metadata and controls
42 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class DateFormatService {
private readonly defaultOptions = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
};
formatDate(date: Date, options: Intl.DateTimeFormatOptions = this.defaultOptions): string {
return new Intl.DateTimeFormat(navigator.language, options).format(date);
}
addDaysToDate(date: Date, days: number): Date {
return new Date(date.getTime() + (days * 24 * 60 * 60 * 1000));
}
formatRelativeTime(date: Date): string {
const current = new Date();
const diff = current.getTime() - date.getTime();
const seconds = diff / 1000;
const minutes = seconds / 60;
const hours = minutes / 60;
const days = hours / 24;
if (days >= 1) {
return `${Math.floor(days)} day(s) ago`;
} else if (hours >= 1) {
return `${Math.floor(hours)} hour(s) ago`;
} else if (minutes >= 1) {
return `${Math.floor(minutes)} minute(s) ago`;
} else {
return `${Math.floor(seconds)} second(s) ago`;
}
}
}