Reputation: 1041
The date format which I am getting from the Api response is like this.
"startDate" : "07112018 00:00".
I want to convert this to in dd-mm-yyyy HH:mm format. that is like this 07-11-2018 00:00.
Can anyone please tell me how to do this using Angular 6.
Upvotes: 0
Views: 1742
Reputation: 2643
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
Upvotes: 1
Reputation: 1
try this function to convert time format as you require
timeFormetChange(){
var date =new Date();
var newDate = date.getDate();
var newMonth = date.getMonth();
var newYear =date.getFullYear();
var Hour = data.getHours();
var Minutes = data.getMinuets();
return `${newDate}-${newMonth }-${newYear} ${Hour}:${Minutes }`
}
Upvotes: 0