开发者

TypeScript Date type is not JS Date type?

I have a model:

export class SendModel{
periodFrom: Date,
periodTo: Date
}

I have a service:

public async Calculate(periodFrom: Date,  periodTo: Date):Promise<SendModel>
{
let resObj = this.http.get(url, params:{periodFrom: `${periodFrom}`, periodTo: `${periodTo}`}).toPromise();
//...another code...//
}

I receive a SendModel object from api.

{
periodFrom: '2022-11-24T00-00-00Z',
periodTo: '2026-01-01T00-00-00Z'
}

Ok, I need add 3 mont开发者_JS百科h to periodFrom var.

let startPayday = receiveObj.periodFrom.setMonth(receiveObj.periodFrom.getMonth()+3); //not working, despite receiveObj.periodFrom has type is Date (what?)
let startPayday2 = new Date(new Date(receiveObj.periodFrom).setMonth(new Date(receiveObj.periodFrom).getMonth()+3)); //worsk! ok

Ok, let`s send it to api.

this.service.Calculate(startPayday2, receiveObj.periodTo); //error!

Wrong api call, status 400, One or more validation errors. Look at the request: api/calculate?periodFrom=Fri Feb 24 2023 03:00:00 GMT 0300 (blablabla)&periodTo=2026-01-01T00-00-00Z

Maybe like this?

this.service.Calculate(startPayday2.toISOString(), receiveObj.periodTo); //nope, argument is type 'string' bla-bal-bla...

startPayday2 has type Date, API expects type Date, but these are different types that are serialized differently?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜