#Subtract dates, display them in the view in days and hours updating without page refresh goal
27 messages · Page 1 of 1 (latest)
@robust vessel I made a thread just FYI.
So I would replace this transform function with one that takes two dates and converts it to days and hours right?
import { Pipe, PipeTransform } from '@angular/core';
import { formatRelative } from 'date-fns';
@Pipe({ name: 'appRelativeDate', pure: false })
export class RelativeDatePipe implements PipeTransform {
private _nowDate = new Date();
public transform(date: Date): string {
return formatRelative(date, this._nowDate);
}
}
add a second parameter to transform
Oh and use this method to transform it?
i guess. I don't understand what you are doing
I am taking these two dates provided by an API ```
Mon Dec 05 2022 14:04:16 GMT-0800 (Pacific Standard Time)
Fri Dec 02 2022 11:36:02 GMT-0800 (Pacific Standard Time)
In the view, in a circle, and those days and hours should update without page refresh.
That's my goal anyways.
Seems weird to have relative dates not based on the current time. But the good news is that situation is a pure pipe.
It's going to display in a circle like this to reminder a user how much time they have until a filing is due.
It is date due and date.
How do I start considering you have this new information? I feel like I need to somehow subtract the dates to get the difference.
that is what formatRelative does
How do you provide two arguments for something like that in the view? {{x | y}}
@Pipe({ name: 'appRelativeDate', pure: false })
export class RelativeDatePipe implements PipeTransform {
private _nowDate = new Date();
public transform(date: Date, dateTwo: Date): string {
return formatRelative(date, dateTwo);
}
}
It's in the guide
If the pipe accepts multiple parameters, separate the values with colons. For example,
{{ amount | currency:'EUR':'Euros '}}adds the second parameter, the string literal 'Euros ', to the output string. Use any valid template expression as a parameter, such as a string literal or a component property.
The template expression
{{ amount | currency:'EUR' }}transforms the amount to currency in euros. Follow the pipe name (currency) with a colon (:) character and the parameter value ('EUR').
Okay so I need change this method for the difference and return the days/hours?
if that is what you want to have. Probably best you start with just a function to do what you want. Then put it into Angular how it best fits. A Component to create DOM, a Directive to modify existing DOM, a Pipe to transform {{ interpolations }}
function transform(startDate: Date, dueDate: Date): string {
// Fill in code. Use a Date Library like date-fns or something else.
}
if data is always current date really only need to pass due date
How would this look in the view?