How to convert ion-datetime to date only?

Hi Ionites,

I’m having a problem with passing the input datetime of ionic to date field of my API. The error state that the datetime variable I’m passing was in incorrect format. The proper format would be YYYY-MM-DD, however I can’t figure out on how to convert this ion-datetime to date field only. I had tried to change the display format but it seems has nothing to do with the actual value, which I’m still receiving the same format (datetime).

Here is the code for html:

          <ion-item class="first-type">
              <ion-label position="floating" class="label">Date of Birth</ion-label>
              <ion-datetime formControlName="birthdate" id="birthdate" displayFormat="MM/DD/YYYY"></ion-datetime>

This is the error coming from API.

birthdate: Array(1)
0: "Date has wrong format. Use one of these formats instead: YYYY-MM-DD."
length: 1

Sample value of birthdate that I’m passing to API.

birthdate: "2019-11-30T14:42:30.951+08:00"

This is the field from API. Please note that the framework that I’ve been using in my API is django-rest framework

birthdate = serializers.DateField(required=True)

Anyone know how to convert the ion-datetime value to DateField format only? Thanks guys.

I think what you’re looking for is a subset of the problem covered by date-fns format:

$ npm i --save date-fns
$ ts-node
$ > import {format} from "date-fns";
> let birthdate = "2019-11-30T14:42:30.951+08:00";
> format(new Date(birthdate), "yyyy-MM-dd");
1 Like

I gonna try this :slight_smile: thanks stay here for a while.

Hey good idea, its already working :slight_smile: