Skip to content

Commit

Permalink
fix: Handle valid date provided as string danrevah#224
Browse files Browse the repository at this point in the history
It is common use-case to provide date as a valid string. Even the backend responses come as a plain string which represents the date.
  • Loading branch information
rahuldimri authored Jun 14, 2022
1 parent c0737c3 commit 682a87a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ng-pipes/pipes/date/time-ago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export class TimeAgoPipe implements PipeTransform {
];

/**
* @param inputDate: Date | Moment - not included as TypeScript interface,
* @param inputDate: Date | Moment | date string - not included as TypeScript interface,
* in order to keep `ngx-pipes` "pure" from dependencies!
*/
public transform(inputDate: any): string {
if (!inputDate || (!inputDate.getTime && !inputDate.toDate)) {
if (!inputDate || (!inputDate.getTime && !inputDate.toDate && !Date.parse(inputDate))) {
return 'Invalid date';
}

Expand Down

1 comment on commit 682a87a

@Stusaw
Copy link

@Stusaw Stusaw commented on 682a87a Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still treating the input string as a Date in the if clause. Would it not be better to check for 'undefined' or Invalid Date and then use the parsed value further down.

        if (!inputDate || !Date.parse(inputDate)) {
            return 'Invalid date';
        }
		
	const parsedDate = new Date(inputDate);
        const past = parsedDate.toDate ? parsedDate.toDate() : parsedDate.getTime();

Please sign in to comment.