Ion datetime - date extraction problem


#1

hi,
i am trying to get the date in such format:
YYYY-MM-DD
after the user has picked date. is ther other way fer extracting the year,month,day from this date?
tnx.
my code:

html file:

<ion-datetime required [disabled]="dataBoolean" displayFormat="DD-MM-YYYY" pickerFormat="YYYY-MM-DD" [(ngModel)]="accountLabels[val]"></ion-datetime>

ts file:

this.accountLabels["DATE"] = new Date().toISOString();
AND THEN I GET THE DATE TO MY FORMAT LIKE THAT:
 let formDate = (this.accountLabels["DATE"]);
let year=formDate.slice(0,4);
        let month = formDate.slice(5,7);
        let day = formDate.slice(8,10);

#2

I would use date-fns/format.


#3

moment is pretty good for that…


#4

Not really. You have to bloat your app by over 2 megabytes for the privilege of having to create a moment object that you don’t really need or want. With date-fns, you only have to add one single function to your app, which works with either the Date objects or strings that you already have.


#5

That’s an exaggeration. I take your point it’s larger but no where near 2mb.

Do you mean when everything is included like the locales??


#6

I took the number from this issue and admittedly did not read the entire thread before posting. moment is still monolithic and much less amenable to dead code elimination than date-fns is.


#7

Agreed - this non-monolithic approach does produce smaller payloads when they’re most important.


#8

hi,
tnx for comment.

first from my understanding i need to install:
npm install date-fns --save?

and yet, did not really understand how to use it, what i do understand is that i can create a date in this format but does it convert my “formDate” to the format i want? if so how to do it?
this is the code in site:

var eoLocale = require('date-fns/locale/eo')
var result = format(
  new Date(2014, 6, 2),
  'Do [de] MMMM YYYY',
  {locale: eoLocale}
)

well, i need to extract the numbers from my date forst so…it is not realy shortening my way…


#9

I don’t see why. You should just be able to pass format() the string that is bound to the <ion-datetime>.