0

I have a form that should include current date and time what should I do to display them without using pipes?

  • 1
    Possible duplicate of [How to format date as dd/MM/yyyy in Angular 2 using pipes?](http://stackoverflow.com/questions/35754586/how-to-format-date-as-dd-mm-yyyy-in-angular-2-using-pipes) – Rachid O Apr 14 '17 at 15:03

1 Answers1

17

You can use the Angular2 Date Pipe to display a JavaScript Date object. Check the docs (linked) for the correct formatting that you are looking for.

@Component({
  selector: 'date-pipe',
  template: `<div>
    <p>Today is {{today | date}}</p>
    <p>Or if you prefer, {{today | date:'fullDate'}}</p>
    <p>The time is {{today | date:'jmZ'}}</p>
    <p>Finally the date and time is {{today | date:'short'}}</p>
  </div>`
})
export class DatePipeComponent {
  today: number = Date.now();
}
Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51
  • how do i call it in the html file ? – Narjes Ben Slimen Apr 14 '17 at 15:01
  • Instead of the `template` property of the component metadata you would use the `templateUrl` property and set the value equal to the relative path to the HTML file and put the code I have provide above inside of the file. – Teddy Sterne Apr 14 '17 at 15:04