This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
forked from ng-bootstrap/ng-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datepicker-adapter.ts
90 lines (77 loc) · 2.22 KB
/
datepicker-adapter.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {Component, Injectable} from '@angular/core';
import {
NgbCalendar,
NgbDateAdapter,
NgbDateStruct,
NgbDateParserFormatter
} from '@ng-bootstrap/ng-bootstrap';
/**
* This Service handles how the date is represented in scripts i.e. ngModel.
*/
@Injectable()
export class CustomAdapter extends NgbDateAdapter<string> {
readonly DELIMITER = '-';
fromModel(value: string): NgbDateStruct {
let result: NgbDateStruct = null;
if (value) {
let date = value.split(this.DELIMITER);
result = {
day : parseInt(date[0], 10),
month : parseInt(date[1], 10),
year : parseInt(date[2], 10)
};
}
return result;
}
toModel(date: NgbDateStruct): string {
let result: string = null;
if (date) {
result = date.day + this.DELIMITER + date.month + this.DELIMITER + date.year;
}
return result;
}
}
/**
* This Service handles how the date is rendered and parsed from keyboard i.e. in the bound input field.
*/
@Injectable()
export class CustomDateParserFormatter extends NgbDateParserFormatter {
readonly DELIMITER = '/';
parse(value: string): NgbDateStruct {
let result: NgbDateStruct = null;
if (value) {
let date = value.split(this.DELIMITER);
result = {
day : parseInt(date[0], 10),
month : parseInt(date[1], 10),
year : parseInt(date[2], 10)
};
}
return result;
}
format(date: NgbDateStruct): string {
let result: string = null;
if (date) {
result = date.day + this.DELIMITER + date.month + this.DELIMITER + date.year;
}
return result;
}
}
@Component({
selector: 'ngbd-datepicker-adapter',
templateUrl: './datepicker-adapter.html',
// NOTE: For this example we are only providing current component, but probably
// NOTE: you will want to provide your main App Module
providers: [
{provide: NgbDateAdapter, useClass: CustomAdapter},
{provide: NgbDateParserFormatter, useClass: CustomDateParserFormatter}
]
})
export class NgbdDatepickerAdapter {
model1: string;
model2: string;
constructor(private ngbCalendar: NgbCalendar, private dateAdapter: NgbDateAdapter<string>) {}
get today() {
return this.dateAdapter.toModel(this.ngbCalendar.getToday());
}
}