Angular 4 Date Pipe converting wrongly
I resolved the issue by adding a custom pipe.
My custom pipe is based on the solution provided by Birwin. Thanks Birwin.
Here is my custom pipe named UtcDate
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'utcDate'
})
export class UtcDatePipe implements PipeTransform {
transform(value: string): any {
if (!value) {
return '';
}
const dateValue = new Date(value);
const dateWithNoTimezone = new Date(
dateValue.getUTCFullYear(),
dateValue.getUTCMonth(),
dateValue.getUTCDate(),
dateValue.getUTCHours(),
dateValue.getUTCMinutes(),
dateValue.getUTCSeconds()
);
return dateWithNoTimezone;
}
}
And I also used default date pipe to format
{{createdDate | utcDate | date:'short'}}
You can pass another param to date pipe as follows:
{{resultItem.createdDate | date : 'short' : 'UTC'}}
This param can be a timezone like '-0430'
or just 'GMT'
See documentation: https://docs.angularjs.org/api/ng/filter/date