@@ -12,11 +12,38 @@ require('./assets/datetimepicker/bootstrap-datetimepicker.js');
12
12
import { Component , Input , AfterViewInit , ElementRef , EventEmitter , Output , ViewEncapsulation } from '@angular/core' ;
13
13
import { CustomInputComponent , customInputAccessor } from './custom-input' ;
14
14
15
+ ( function ( ) {
16
+ // Format Date into string.
17
+ // month(M)、day(d)、hour(h)、minute(m)、second(s)、quarterly(q) , 1 or 2 character,
18
+ // Samples:
19
+ // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
20
+ // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
21
+ ( Date as any ) . prototype . format = function ( fmt ) { //author: meizz
22
+ var o = {
23
+ "M+" : this . getMonth ( ) + 1 , // Month
24
+ "d+" : this . getDate ( ) , // Day
25
+ "h+" : this . getHours ( ) , // Hour
26
+ "m+" : this . getMinutes ( ) , // Minute
27
+ "s+" : this . getSeconds ( ) , // Second
28
+ "q+" : Math . floor ( ( this . getMonth ( ) + 3 ) / 3 ) , // Quarterly
29
+ "S" : this . getMilliseconds ( ) // Millisecond
30
+ } ;
31
+ if ( / ( y + ) / . test ( fmt ) ) fmt = fmt . replace ( RegExp . $1 , ( this . getFullYear ( ) + "" ) . substr ( 4 - RegExp . $1 . length ) ) ;
32
+ for ( var k in o )
33
+ if ( new RegExp ( "(" + k + ")" ) . test ( fmt ) ) fmt = fmt . replace ( RegExp . $1 , ( RegExp . $1 . length == 1 ) ? ( o [ k ] ) : ( ( "00" + o [ k ] ) . substr ( ( "" + o [ k ] ) . length ) ) ) ;
34
+ return fmt ;
35
+ } ;
36
+ } ) ( ) ;
37
+
38
+ declare interface IDate extends Date {
39
+ format : ( fmt : any ) => string ;
40
+ }
41
+
15
42
@Component ( {
16
43
selector : 'date-time-picker' ,
17
- template : `<input type="text" class="form-control" [disabled]="disabled" [(ngModel)]="model " (blur)="onBlur()" />` ,
44
+ template : `<input type="text" class="form-control" [disabled]="disabled" [(ngModel)]="viewValue " (blur)="onBlur()" />` ,
18
45
styleUrls : [ './assets/datetimepicker/bootstrap-datetimepicker.css' ] ,
19
- encapsulation : ViewEncapsulation . None ,
46
+ encapsulation : ViewEncapsulation . None ,
20
47
providers : [ customInputAccessor ( DateTimePickerComponent ) ]
21
48
} )
22
49
// ControlValueAccessor: A bridge between a control and a native element.
@@ -75,11 +102,14 @@ export class DateTimePickerComponent implements AfterViewInit {
75
102
format, // output format
76
103
minView : parseInt ( this . minView , 10 ) || minView || 0 , // min view (default: hour view)
77
104
} )
78
- . on ( 'hide' , ev => {
79
- this . viewValue = $ ( ev . target ) . val ( ) ; // set value. (calling onChange())
80
- this . model = this . useTimestamp ? ( new Date ( this . model ) . getTime ( ) / 1000 ) : this . viewValue ;
81
- this . onSelect . emit ( { timestamp : this . model , text : this . viewValue } ) ; // change event callback of input
82
- } ) ;
105
+ . on ( 'hide' , ev => {
106
+ this . viewValue = $ ( ev . target ) . val ( ) ; // set value. (calling onChange())
107
+ // If use timestamp, change string into timestamp.
108
+ //
109
+ this . model = this . useTimestamp ? ( Date . parse ( this . viewValue ) / 1000 ) : this . viewValue ;
110
+ this . onChange ( this . model ) ;
111
+ this . onSelect . emit ( { timestamp : this . model , text : this . viewValue } ) ; // change event callback of input
112
+ } ) ;
83
113
}
84
114
85
115
// Set touched on blur
@@ -89,10 +119,10 @@ export class DateTimePickerComponent implements AfterViewInit {
89
119
90
120
// Write a new value to the element.
91
121
writeValue ( value : string ) : void {
92
- if ( value !== this . model ) {
122
+ if ( value && value !== this . model ) {
93
123
this . model = value ;
94
124
const vd = new Date ( ( + value ) * 1000 ) ; // multiply 1000 for server timestamp
95
- this . viewValue = vd . format ( this . formatStr ) ;
125
+ this . viewValue = ( vd as IDate ) . format ( this . formatStr ) ;
96
126
}
97
127
}
98
128
0 commit comments