I need to change my date format, the problem is that I can't use moment.js, I need just to transform date from yyyy-mm-dd to dd-mm-yyyy. I use angular v 2.4.0.
Import DatePipe from angular/common and then use the below code: var datePipe = new DatePipe(); this. setDob = datePipe. transform(userdate, 'dd/MM/yyyy');
You can format date/time in TypeScript, by using any of the built-in methods on the Date object or creating a reusable function that formats the date according to your requirements by leveraging built-in methods like getFullYear , getMonth , etc.
Use DatePipe
pipe
date_expression | date[:format]
in your case
{{ date_expression | date:'dd-MM-yy' }}
How to use the pipe inside the component :
NgModule({
....
providers: [DatePipe]
})
or
@Component({
....
providers: [DatePipe]
})
In you component set it as a date
variable
constructor(private datePipe: DatePipe) {
}
ngOnInit() {
this.date = this.datePipe.transform(new Date(), 'dd-MM-yy');
}
You can do it creating custom Pipe. Refer below code. For more info refer DatePipe Documentation.
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({
name: 'customDateFormat',
})
export class customDateFormatPipe implements PipeTransform {
transform(value: string) {
var datePipe = new DatePipe("en-US");
value = datePipe.transform(value, 'dd-mm-yyyy');
return value;
}
}
Add custom pipe in html as shown below:
{{currentDate | customDateFormat }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With