简体   繁体   English

ion-datetime绑定到时代

[英]ion-datetime bind to epoch

I have a model that requires the date/time as milliseconds in Unix Epoch format. 我有一个需要Unix Epoch格式的日期/时间为毫秒的模型。 I have tried using the Moment interface, Date | 我尝试使用Moment界面,Date | numeric as type and I can't seem to get it right. 数字类型,我似乎无法正确理解。

I want the control to display in human readable format and the picker likewise but I want the databound model to be numeric. 我希望控件以人类可读的格式显示,并且选择器也同样显示,但我希望数据绑定模型为数字。 I can't use a pipe ("Cannot have a pipe in an action expression"). 我不能使用管道(“动作表达式中不能有管道”)。 Should I remove the two way databinding, translate the value in the changeModel function and populate the person.date_of_birth with a similar function? 是否应该删除双向数据绑定,转换changeModel函数中的值,并使用类似的函数填充person.date_of_birth?

.html html的

<ion-datetime displayFormat="MMM DD, YYYY" pickerFormat="MMM DD, YYYY" (ngModelChange)="changeModel($event)" [(ngModel)]="person.date_of_birth"></ion-datetime>

.ts: .TS:

let person={name: string, date_of_birth: numeric};

The model is written to a local database on the mobile (pouchdb/sqlite) and then synched with a mongodb database via nodejs REST API. 该模型将写入移动设备上的本地数据库(pouchdb / sqlite),然后通过nodejs REST API与mongodb数据库同步。 It is only displayed on this one html page so I'd really like it to be numeric everywhere else. 它仅显示在该html页面上,所以我真的希望它在其他任何地方都是数字。

As input for your ion-datetime , you can use public yourDate: string = new Date().toISOString(); 作为ion-datetime输入,您可以使用public yourDate: string = new Date().toISOString(); . So this is the value you want to bind to your ion-datetime . 因此,这是您要绑定到ion-datetime

If you want to have another format, you can do something like this new Date(yourDate).getTime() . 如果要使用其他格式,则可以执行类似此new Date(yourDate).getTime() If you have this ISOString , you can always parse it back to a Date object. 如果具有此ISOString ,则始终可以将其解析回Date对象。

Update 更新

Working with a pipe and a format function. 使用管道和格式功能。

Here we have a one-way databinding that is using my custom date pipe, thats formatting an numeric date to an ISOString . 在这里,我们使用我的自定义date管道进行单向数据绑定,即将数字日期格式化为ISOString The (ngModelChange) event is the "other-way" binding, thats assigning back a numeric value to date_of_birth (format is a custom function). (ngModelChange)事件是“其他”绑定,也就是将数字值分配回date_of_birth (格式是自定义函数)。

page.html page.html中

<ion-datetime displayFormat="MMM DD, YYYY" 
    (ngModelChange)="date_of_birth=format($event)" 
    [ngModel]="date_of_birth | date"></ion-datetime> 

date.pipe.ts date.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'date'
})
export class DatePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return new Date(value).toISOString();
  }

}

page.ts page.ts

date_of_birth: number = new Date().getTime();

format(val) {
  return new Date(val).getTime();
}

Working StackBlitz 工作StackBlitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM