date.getDate 不是函数 Typescript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57007749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:50:28  来源:igfitidea点击:

date.getDate is not a function Typescript

angulartypescriptdatedatetime

提问by Natesh

I have 3 input controls two date type and one is number, what i wanna to do is get the date from 1st control and no. of days from other controls add them both and assign it to third date control in typescript. but i gives me error here is my code:

我有 3 个输入控件,两个日期类型,一个是数字,我想要做的是从第一个控件获取日期,而没有。来自其他控件的天数添加它们并将其分配给打字稿中的第三个日期控件。但我给我的错误是我的代码:

activeFrom: Date;
activeNoDays: number;

    //UpdateExpiry Function is called by textbox event
        updateExpiry=():void =>{
             this.gDetailDS = this.gDetailForm.value;
            console.log("Expiry days:"+this.gDetailDS.activeNoDays);
            console.log (this.addDays(this.gDetailDS.activeFrom,this.gDetailDS.activeNoDays))
          }

          addDays(date: Date, days: number): Date {
            console.log('adding ' + days + ' days');
            console.log(date);
            date.setDate(date.getDate() + days);
            console.log(date);
            return date;
          }

Here is HTML Controls:

这是 HTML 控件:

<input
     id="txtActivateFrom"
     type ="date"
     min="rdMinDate"
     formControlName="activeFrom"
     class="form-control"
     value="{{ this.gDetailDS.activeFrom | date:'yyyy-MM-dd' }}"
     displayFormat="yyyy-MM-dd"
     useValueAsDate />

<input  type="number"
        formControlName="activeNoDays" class="form-control"
        (change)="updateExpiry()"/>

console Messages:

控制台消息:

Expiry days:25
adding 25 days
2019-07-12

I have tried everthing but still getting this issue :

我已经尝试过一切,但仍然遇到这个问题:

ERROR TypeError: date.getDate is not a function

错误类型错误:date.getDate 不是函数

link: screenshot of error appear on browser console

链接:浏览器控制台上出现的错误截图

回答by LeBavarois

I once had a similar problem when getting dates from a web API. I solved it by creating a new Date object.

从 Web API 获取日期时,我曾经遇到过类似的问题。我通过创建一个新的 Date 对象来解决它。

So your function call could look like this:

所以你的函数调用可能是这样的:

this.addDays(new Date(this.gDetailDS.activeFrom),this.gDetailDS.activeNoDays)

回答by Stevenfowler16

You need to create the Date object before trying to pass it into the addDaysfunction. Input.valueof type="date"will return a string representation and not a date object.

您需要先创建 Date 对象,然后再尝试将其传递给addDays函数。Input.valueoftype="date"将返回字符串表示而不是日期对象。

If this is received over the wire via JSON you will also still need to create the date object when doing JSON.Parse()

如果这是通过 JSON 通过线路接收的,您在执行 JSON.Parse() 时仍然需要创建日期对象

fiddle: https://jsfiddle.net/o7y6u2zL/4/

小提琴:https: //jsfiddle.net/o7y6u2zL/4/

let date = document.getElementById("start");
console.log(date.value);
console.log(typeof date.value);
let realDateObject = new Date(date.value);
console.log(typeof realDateObject)
<label for="start">Start date:</label>

<input type="date" id="start" name="trip-start"
       value="2018-07-22"
       min="2018-01-01" max="2018-12-31">