You can use DatePipe API for this: https://angular.io/api/common/DatePipe
@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:'h:mm a z'}}</p> //format in this line
</div>`
})
// Get the current date and time as a date-time value.
export class DatePipeComponent {
today: number = Date.now();
}
and this is what actually you want:
{{myDate | date: 'dd-MM-yyyy'}}
Edit: Using built-in pipe in component,
import { DatePipe } from '@angular/common';
class MyService {
constructor(private datePipe: DatePipe) {}
transformDate(date) {
return this.datePipe.transform(date, 'yyyy-MM-dd');
}
}
Please read this topic, I took example from there: https://stackoverflow.com/a/35152297/5955138
Edit2:
As I described in comment, you can substring your date using this.
var str = '07112018'
var d = new Date(str.substring(0, 2)+'.'+str.substring(2, 4)+'.'+str.substring(4, 8));
Output: Wed Jul 11 2018 00:00:00 GMT+0300