To format only the output of a variable, you should use pipes.
The Data Pipe can be found on the docs.
Basically, on your component.html
, you have to do:
<p>The date is {{ datepicker.value | date:'yyyy/MM/dd' }}</p>
This will display the datapicker.value
formatted as yyyy/mm/dd
. You have a lot of other options to format your output based on pipes and even create your owns.
To format the date on the .ts
file, you have a couple of options.
- Use the Date Pipe from Angular.
import { DatePipe } from '@angular/common';
new DatePipe('en').transform(this.datepicker.value, 'yyyy/MM/dd');
- Use dateformat (it changes the prototype of
Date
object, adding a format
method)
- Use date-fns
- Use moment.js (overkill, super heavy for such small use)