IOnic 2 Rc1: How to convert date into this 'yyyy-MM-dd' format in angular 2


#1

i want to convert current data into ‘yyyy-MM-dd’ format in .ts file. i template it can easily be done by using data pipe. how to do that in typescript.

In template:

{{date | date:‘yyyy-MM-dd’}}

How to convert in this format ‘yyyy-MM-dd’ in typescript.

now i am just getting current date by using this.

this.date = new Date(); but need to convert it into given format. Please guide how to do that… Thanks!


#2

You can use some good old javascript to handle the data, seems like the pipe is pretty unstable. What I did was just:

        //Gets it as YYYY-MM-DD
        var dateData = this.s.depatureDate.split('-');
        var year = dateData [0];
        var month = dateData [1];
        var day = dateData [2];
        var dateFormated = day + "/" + month + "/" + year;

#3

I strongly suggest using moment for all date and time handling.


#4

What i did was.

this.date=new Date();

this.today_date = this.date.getFullYear().toString()+’-’+(this.date.getMonth()+1).toString()+’-’+this.date.getDate().toString();