-1
  time = new Date();
  timeEntered1 = new Date();

  getTime() {
    console.log(this.timeEntered1);
    console.log(this.time);
    console.log(this.timeEntered1.getTime());
    console.log(this.time.getTime());
  }

for console.log(this.time.getTime()); I got an error which "is TypeError: this.timeEntered1.getTime is not a function".

time is a simple Date() variable and timeEntered1 is a Date() which the user enter in my html code, and I got it using two way binding. So I do not get why I can use .getTime with time and why I can not with timeEntered1 since they are both a Date().

Here is my html code

  <ion-content padding>
    <ion-datetime  
    displayFormat="HH:mm" 
    [(ngModel)]='timeEntered1' 
    picker-format="HH:mm" 
    >
    Choose time: </ion-datetime>
      <ion-button (click)="getTime()">Pick Time</ion-button>
  </ion-content>
YLM
  • 335
  • 6
  • 17

1 Answers1

1

ion-datetime returns the Date as string. So you do not need to instantiate the var time.

You should convert the Datetime string to Date like this.

ionDateString = '1968-11-16T00:00:00' 
newDate = new Date(dateString);

The ion-datetime is returning the Date in HH:mm format as string, to convert into Date() type try this:

getTime() {
let myDate = new Date(Date.prototype.setHours.apply(new Date(), this.timeEntered1.split(':')));

console.log('CONVERTED', myDate);
}
  • I already tried it, but I got Invalid Date if I console.log your newDate – YLM Sep 16 '19 at 12:59
  • now i see... you want only the hours in the Date format right? try this: let myDate = new Date(Date.prototype.setHours.apply(new Date(), this.timeEntered1.split(':'))); – Raphael Pinheiro Sep 16 '19 at 13:42
  • Thanks, it works. I still get Property 'split' does not exist on type 'Date'; but it works anyway.. – YLM Sep 16 '19 at 13:57
  • Just remove the new Date(); from timeEntered1 = new Date(); Type it as string and the error will be gone. – Raphael Pinheiro Sep 16 '19 at 14:03
  • thanks, now I do not have the error, but I got a Nan value when I console.log ... – YLM Sep 16 '19 at 14:09