How to set current date ion-datepicker

I want to set date by default current date

code that i used

Select Date

this.dt = new Date();
this.myDate = ‘’ + this.dt.getFullYear() + ‘-’ + ‘’ + this.dt.getDate() + ‘-’+ ‘’ + this.dt.getUTCMonth();

if i set date - 2016-29-7 – no default date selected
if i set date - 2016-29-07 – undefined,06 2016
if i set date - 2016-9-7 – no default date display

you could do the following in your markup:

  <ion-datetime displayFormat="MM DD YYYY" pickerFormat="DD MMM YYYY" [(ngModel)]="myDate"  ></ion-datetime>

and in the class definition file i.e *.ts / *.js do the following:

      myDate:any  = new Date().toISOString();
1 Like

thankx buddy…

why use toISOString()??

1 Like

The toISOString() method converts a Date object into a string, using the ISO standard (i.e. ISO-8601) and the format is: YYYY-MM-DDTHH:mm:ss.sssZ

For example:

2016-07-29T12:50:45.070Z

1 Like

any specific requirement for that? if i am using only toString() it’s not worked why?

consider a simple example:

in home.ts

date:any = new Date().toString();

in home.html

<p>{{ date }}</p>

the output is :

Fri Jul 29 2016 19:15:59 GMT+0530 (IST)

thanx vaibhav_bista

i try your code and run proper but problem is that when i tried to fetch selected date from datepicker i got below format.

2016-08-05T10:15:12.489Z

but i want DD/MM/YYYY
how i do that?

Sorry for reaching late.

First approach:
if you are creating date object using Date() in .ts file.

today: any = new Date();
console.log(today); // Thu Aug 04 2016 12:51:09 GMT+0530 (IST)

then it’s easy to convert to a mm/dd format using pipes provided by angular2 i.e. in .html file

<p>{{today | date:"MM/dd/yyyy"}}</p>

whose output is => 08/04/2016

Second approach:
in .ts file

date: any = new Date().toISOString(); // 2016-08-04T07:43:09.222Z

this.year = this.date.split("-")[0];
this.month = this.date.split("-")[1] - 1;
this.day = ( this.date.split("-")[2] ).split("T")[0];

this.today = new Date(this.year, this.month, this.day);

in .html file

<p>{{today | date:'MM/dd/yyyy'}}</p> // 08/04/2016

find some articles related to date pipes or filter in angular2 that might help you.

also, ask for other simplified/optimized solutions on this question from other developers.

and check whether is there any such filters or pipes are available in angular2 to convert a date in a particular format from fetched date-picker string effectively.

EDIT :
is there any way to get the date into dd/MM/yyyy format…

I tried this

<p>{{ today | date:'dd/MM/yyyy' }}</p>

but the output is same as MM/dd/yyyy

thanks vaibhav

I also no idea for get output in dd/MM/yyyy format.

i have one link might be help you

https://angular.io/docs/ts/latest/api/common/index/DatePipe-class.html

and vaibhav
i have one another issue please checked it out.