无法调用网络API,因为未获取正确格式的日期来添加网址

时间:2018-11-19 07:14:40

标签: angular api date

每当我尝试使用datepicker中的值调用Web api时,它都会以一种奇怪的方式显示日期,从而导致服务器请求错误。

获取https://lihcode-lufthansa-open-new-v1.p.mashape.com/v1/operations/schedules/sfo/bom/1542738600000?limit=10 400(错误请求)。

 Here date is 154273860000, but I want date in 2018-11-20(yyyy-mm-dd).

这是我的日期HTML组件。

<input required matInput #input maxlength="3" 
name="destination" placeholder="Enter destination" 
class="form-control 
[(ngModel)]="flightSchedule.destination">
<mat-hint align="end">Destination airport.             
</mat-form-field>

我的模态类是:

export class FlightSchedule {

    constructor(
        public destination: string,
        public origin: string,
        public date: string,
        public limit: string
    ) {}

}

我需要帮助,请有人帮助我

预先感谢

1 个答案:

答案 0 :(得分:0)

已编辑

尝试格式化ngModelChange事件上的日期,但是您必须分配给另一个属性(formatedDate)并从formatedDate属性获取日期。

<input required matInput #input maxlength="3" 
name="destination" placeholder="Enter destination" 
class="form-control 
[(ngModel)]="flightSchedule.destination"
(ngModelChange)="formater(flightSchedule.date)"> <--- here

添加新属性formatedDate

export class FlightSchedule {
  constructor(
    public destination: string,
    public origin: string,
    public date: string,
    public formatedDate?: string, <--- here
    public limit: string
  ) {}
}

在课堂上添加一个函数

formater(unixTime: number) {
  var date = new Date(unixTime);  
  var myDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}T${date.getHours()}:${date.getMinutes()}`;
  this.flightSchedule.formatedDate = myDate;
}

一会儿

formater(unixTime: number) {
  const date = new Date(unixTime);  
  const myDate = moment(date).format('YYYY-MM-DDTHH:mm);
  this.flightSchedule.formatedDate = myDate;
}