Kuldeep
Kuldeep

Reputation: 659

How to get Date Object in Ionic 4

I want to get Date Object in Ionic 4, but i am getting Date not found, i am not able to compile the code.

import { Component, OnInit } from '@angular/core';


@Component({
  selector: 'app-bookingdetail',
  templateUrl: './bookingdetail.page.html',
  styleUrls: ['./bookingdetail.page.scss'],
})
export class BookingdetailPage implements OnInit {
   d = new Date();
}


Upvotes: 1

Views: 4346

Answers (3)

Emmanuel Iyen
Emmanuel Iyen

Reputation: 439

export class BookingdetailPage implements OnInit {
  public currentDate: String;
  date: number;
}

constructor(){ 
 this.date = new Date().getTime();
 this.currentDate = (new Date()).toISOString();
 }

Upvotes: 0

horelvis
horelvis

Reputation: 124

For setting a component in IONIC 4 use toISOString function :

<ion-datetime  [value]="today"></ion-datetime>

ngOnInit() {
   const now = new Date();
   this.today = now.toISOString();
}

Upvotes: 1

Tomas Vancoillie
Tomas Vancoillie

Reputation: 3868

Assign the variable to the Date type. Then for the easiest-of-uses: you can just console log the result when the page is loaded.

export class BookingdetailPage implements OnInit {
   d: Date = new Date();
}     

ngOnInit() {
   console.log(this.d);
}

OUTPUT (in console)

Mon Jan 21 2019 08:35:52 GMT+0100 (Midden-Europese standaardtijd)

Upvotes: 0

Related Questions