Skip to content

Be able to customize the message of the Relative Time (timeago) pipe  #441

@Jukien

Description

@Jukien

Description
Depending on the application and the usage, we want to change the message displayed for the relative time.
For my case, I would like different messages like this:

if (seconds < 30) { // less than 30 seconds ago will show as 'Just now'
  return "à l'instant"; // return 'Just now';
} else if (seconds < 60) {
  return "il y a moins d'une minute"; // return 'Less than a minute'
} else if (seconds < 120) {
  return 'il y a 1 minute'; // return 'A minute ago'
} else if (seconds < intervals.hour) {
  return 'il y a ' + new DatePipe('fr-FR').transform(ms, 'm') + ' minutes'; // return 'x minutes ago'
} else if (seconds <= intervals.day) {
  return "aujourd'hui à " + new DatePipe('fr-FR').transform(ms, 'H:mm'); // return 'today at hh:mm'
} else if (seconds <= intervals.day * 2) {
  return "hier à " + new DatePipe('fr-FR').transform(ms, 'H:mm'); // return 'yesterday at hh:mm'
} else {
  if (new Date().getFullYear() === new Date(ms).getFullYear()) {
    return "le " + new DatePipe('fr-FR').transform(ms, 'd MMM à H:mm'); // return 'on 3 July at 15:02' (example)
  } else {
    return "le " + new DatePipe('fr-FR').transform(ms, 'd MMM y à H:mm'); // return 'on 2 March 2023 at 12:40' (example)
  }
}

Proposed Solution
I don't know how to exactly do this but we can imagine a class to implement custom messages like my code above where we have milliseconds in parameter and a string to return.

Alternatives considered
Have properties like:

message.lessThan30secondes = "Just now"
message.lessThan1hour = "{time} minutes ago"
message.lessThan1day = "today at {time}"

But I think there will be limitations with this solution, if we want to change the format of time and date for example.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions