Ion-datetime format

hello, i’m using this ion-datetime amd i want to get date like 12/03/2018, any help please ?

Follow link
DatePipe https://angular.io/api/common/DatePipe

how to use it in the html file

<ion-datetime [(ngModel)]></ion-datetime>
<ion-item>
  <ion-label>Date</ion-label>
  <ion-datetime displayFormat="MM/YYYY" pickerFormat="MMMM YYYY" [(ngModel)]="myDate"></ion-datetime>
</ion-item>

it doens’t work, already use it

<ion-item>
      <ion-label>From </ion-label>
      <ion-datetime pickerFormat="D/M/YYYY" [(ngModel)]="from_date" ></ion-datetime>
    </ion-item>

send your code both .html & .ts

html

  <ion-list>
    <ion-item>
      <ion-label>From </ion-label>
      <ion-datetime pickerFormat="D/M/YYYY" [(ngModel)]="from_date" ></ion-datetime>
    </ion-item>
    <!-- to -->
    
    <ion-item>
        <ion-label>To </ion-label>
        <ion-datetime displayFormat="D/M/YYYY" pickerFormat="D/M/YYYY" [(ngModel)]="to_date" ></ion-datetime>
    </ion-item>
  </ion-list>

and .ts, it where i want to get the date like on my question

share your ts file also

 from_date:any
  to_date:any
  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

can you format it please ?

click on given link they display in blue color .

let me try this. thanks

please mark as solution so easily identified by other

i get it like 2018-05-02
how to get it like Day/Mounth/Year

Use moment.js it will format for you the dates how you would like to customize it. And use this code to achieve that console.log(moment(date).format("DD/MM/YYYY")); Good to go :slight_smile:

1 Like

I agree with what @gbrigens says, but that doesn’t work, i’ll show my code:

HTML:

<ion-card>
        <ion-item>
            <ion-label>Sate date</ion-label>
            <ion-datetime displayFormat="MMM DD YYYY" [(ngModel)]="date" (ionChange)="parseDate()"></ion-datetime>
        </ion-item>
    </ion-card>
    <ion-card>

TS:

  date: string;

  constructor(public navCtrl: NavController) {
 
    var date_to_parse = new Date();
    var year = date_to_parse.getFullYear().toString();
    var month = (date_to_parse.getMonth() + 1).toString();
    var day = date_to_parse.getDate().toString();

    this.date = year + '/' +  month + '/' + day;

    this.parseDate();
  }

  parseDate(){ 
    var firstStape, secondStape;
    if (this.date.substring(5,6) === "0"){
        firstStape = this.date.substring(0,5);
        if (this.date.substring(8,9) === "0"){
            firstStape = firstStape + this.date.substring(6,8);
            secondStape = this.date.substring(9);
        }
        else{
            secondStape = this.date.substring(6);
        }
        this.date = firstStape + secondStape;
    }
     
    this.date = this.date.replace('-','/');
}

I hope this helps you

1 Like

I’m sorry, but this is crazy. You do not want to be doing all this brittle, ugly string manipulation at all, let alone on every single change event.

Ionic’s datetime picker deals in ISO8601 strings, which is a good thing. Simply let it do its job:

<ion-datetime [(ngModel)]="date">
date = new Date().toISOString();

All the formatting should be done away from the interaction with the date picker component, and the best way to do it is using date-fns, not moment, because it is lighter and deals directly with ISO8601 strings:

declare var require(mn: string): any;
let format = require("date-fns/format");
getDisplayDate(): string {
  return format(this.date, "D/MM/YYYY");
}
1 Like

Have you try to read Date Time Format? Do you understand what is displayFormat and pickerFormat? It’s better if you read the documentation for more info.

As for above, it should look like this

<ion-item>
  <ion-label>From</ion-label>
  <ion-datetime displayFormat="DD/MM/YYYY" pickerFormat="DD/MM/YYYY" [(ngModel)]="from_date" ></ion-datetime>
</ion-item>

change your display format: displayFormat=“MMM/DD/YYYY” you need SLASHES