将数组绑定到“datetime-local”?

Bind array to `datetime-local`?

我有一项服务可以提取表示为数组的日期时间 (e.g. [2021, 09, 02, 08, 46] or [yyyy, mm, dd, hh, mm])

在我的组件中,以下 属性 存储上面提取的日期时间

public startTime : number[];

在我的模板中,我想将此数组绑定到我的输入,如下所示:

<input type="datetime-local" [ngModel]="startTime" />

是否有一种干净的方法来将 startTime 表示为数组 startTime 绑定到 datetime-local?如果不需要,我不想做一堆转换或转换。

我想你可能需要一些铸造。

const startTime = new Date(...startTimeArray);

有了 Date 对象,您可以随心所欲地玩弄它:

<input type="datetime-local" [ngModel]="startTime | date:'yyyy-MM-ddTHH:mm'" (ngModelChange)="startTime = parseDateEvent($event)"/>
parseDateEvent(date: string) {
   return !!date ? new Date(date) : null;
}