-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular2-date-adapter.ts
More file actions
45 lines (41 loc) · 1.38 KB
/
angular2-date-adapter.ts
File metadata and controls
45 lines (41 loc) · 1.38 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
43
44
45
import {MatDateFormats, NativeDateAdapter} from "@angular/material";
export const ISO8601_DATE_FORMATS = {
parse: {
dateInput: {month: 'short', year: 'numeric', day: 'numeric'}
},
display: {
dateInput: 'ISO8601',
monthYearLabel: {year: 'numeric', month: 'short'},
dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
monthYearA11yLabel: {year: 'numeric', month: 'long'},
}
};
export const DDMMYYYYY_DATE_FORMATS = {
parse: {
dateInput: {month: 'short', year: 'numeric', day: 'numeric'}
},
display: {
dateInput: 'DDMMYYYY',
monthYearLabel: {year: 'numeric', month: 'short'},
dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
monthYearA11yLabel: {year: 'numeric', month: 'long'},
}
};
export class Angular2DateAdapter extends NativeDateAdapter{
format(date: Date, displayFormat: Object): string {
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
switch (displayFormat){
case "DDMMYYYY":
return Angular2DateAdapter._to2digit(day) + '/' + Angular2DateAdapter._to2digit(month) + '/' + year;
case "ISO8601":
return Angular2DateAdapter._to2digit(day) + '/' + Angular2DateAdapter._to2digit(month) + '/' + year;
default:
return date.toDateString();
}
}
private static _to2digit(n: number) {
return ('0' + n).slice(-2);
}
}