How to do Date Format under functional part in ionic2 App

Hi All,

my requirement is how can we format the date, functionality wise in ts file. we can do this in html file side, like this

{{ date | ‘yyyy MM dd’}}

same thing how can we achieve from functional side?


You can use moment js, check here the docs

import * as moment from 'moment';

Momentjs is a great library.

You can create your own date formatting utilities too.

function pad(num) {
  str = num.toString();
  if (str.length === 1) {
    return '0' + str;
  return str;

// the date you want to format

var date = new Date(;

// get day

var d = date.getDay();

d = pad(d);

// get month

var m = date.getMonth();

m = pad(m);

// get the year

var y = date.getFullYear();

// stitch it all together

var str = y + ' ' + m + ' ' + d;

// test


Ok, how can i achieve the same using pipes, can some one explain , every one creating pipes and using at html side , how we use pipes at functionality wise (in controller)…

Don’t quote me, but I think pipes are only meant to be used in templates.

Momentjs has format functions that perform the same function as the DatePipe, if that is the way you want to go.

date-fns > moment

Works directly with ISO8601 strings or Date objects, no need for a “moment” object that we don’t really want, much more amenable to dead code elimination.