I am using the Angular DatePipe to format my dates I am getting from an API call, but it doesn’t work only on Safari. It works as expected on every other browser. Here is the Error that I am getting.
Runtime Error:
InvalidPipeArgument: '2015-02-26 14:10:43' for pipe 'DatePipe'
Any suggestions on how to fix this?
My suggestion would be "don’t use the DatePipe
". It’s trivial to roll your own that is not dependent on browser support:
declare function require(name:string): any;
let format = require('date-fns/format');
@Pipe({
name: 'dateformat'
})
@Injectable()
export class DateFormatPipe implements PipeTransform {
transform(d: Date | string, fmt: string): string {
let rv = format(d, fmt);
return rv;
}
}
1 Like
I am actually Implementing what you’ve suggested and I am almost done. I am using your code, right now it is transforming the string from 2015-02-26 14:10:43 to this format 2016-03-02T18:56:01.000-05:00. How can I format it to 6/16/2017, 3:45AM? Thank you for the help
The second fmt
parameter is the desired format. You can see how to build it here. Sample usage syntax:
{{starting | dateformat:'YYYY年M月D日'}}
Thanks A lot ! Just in case anyone is wondering Here is what I changed to get the desired format
let rv = format(d, fmt='YYYY-MM-DD');
It’s intended so that the same pipe can be used with different formats in different places in your application. Instead of hardcoding the format string in the pipe, pass an argument from the template as demonstrated in my previous post.
Thank for that note, It just made my life a lot easier, I was going hard code it otherwise 