It is possible to bind a component property of Date type to a HTML5 input with type set to datetime-local?
In my component I have a poperty:
<
I was looking into this problem as well and started to go down this examples road. However, you can use [(ngModel)] on an input of the type [date,datetime,datetime-local]. The key is to match the expected format the control is expecting. In this case it expects this format. Which also means the type that you bind to the control needs to be a string. I have provided an example plunker, that demonstrates how to use [(ngModel)].
import { Component } from 'angular2/core';
@Component({
selector: 'my-app',
template: `
`
})
export class AppComponent {
private _dateTimeLocal: Date;
constructor() {
this._dateTimeLocal = new Date();
}
private parseDateToStringWithFormat(date: Date): string {
let result: string;
let dd = date.getDate().toString();
let mm = (date.getMonth() + 1).toString();
let hh = date.getHours().toString();
let min = date.getMinutes().toString();
dd = dd.length === 2 ? dd : "0" + dd;
mm = mm.length === 2 ? mm : "0" + mm;
hh = hh.length === 2 ? hh : "0" + hh;
min = min.length === 2 ? min : "0" + min;
result = [date.getFullYear(), '-', mm, '-', dd, 'T', hh, ':', min].join('');
return result;
}
public set dateTimeLocal(v: string) {
let actualParsedDate = v ? new Date(v) : new Date();
let normalizedParsedDate = new Date(actualParsedDate.getTime() + (actualParsedDate.getTimezoneOffset() * 60000));
this._dateTimeLocal = normalizedParsedDate;
}
public get dateTimeLocal(): string {
return this.parseDateToStringWithFormat(this._dateTimeLocal);
}
}