I'm working on an Angular 8+ project (so I'm using Typscript) and I have a table with some data and I need to display difference of time between two date for each row. On the project I'm using Dayjs.
At the beginning I was using this function:
calculateDelay(alertDate: string, operationDate: string): string {
if (alertDate && operationDate) {
return dayjs(alertDate).from(operationDate, true);
}
}
and it returned data like this (2 days
- 2 years
- one day
....)
but there is a problem, with this code, when there is a 1 (day year or whatever) it's written in text and not with number and I would like number only.
(The function should return a string but I want this format 1 year
and not one year
).
So I tried this
calculateDelay(alertDate: string, operationDate: string): string {
if (alertDate && operationDate) {
const date: string = dayjs(alertDate).from(operationDate, true);
const [value, unit]: string[] = date.split(" ");
return isNaN(+value) ? "1 " + unit : value + " " + unit;
}
}
(isNaN is from loadash)
It work well but there is a problem... with second the output of the first function is some seconds
and so my new code return 1 seconds
with an error of grammar (since there is only one second).
I know there is a methode called .format()
with day js but I just don't know how to use it properlly since the format isn't really fix (I want the result in month, years, minutes when it could be relevant).
Can some please help me ?
Also I would like to avoid custom parsing and deleting useless s at the end of the string (if possible).
Thanks in advance
As I investigated the repository, you can use the plugin UpdateLocale and override the default config. There is an example for you. DayJs locale override