How to get formatDistanceToNowStrict result into a specific format? #3661
-
I need to get the difference between two dates with a specific format (y + 'yrs' + m + 'mos'). I found date-fns and it works very cool, with the formatDistanceToNowStrict function it automatically gives me the difference between the date I put in and today's date, the problem is that it only gives me either the years (rounded) or the exact months, but I can't get the format I need. Does anyone know how or how else I can achieve this? First image: Basically I need to get the difference between TODAY and the custom date (red box) and paint the result as in the blue box |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'd recommend you leverage either intervalToDuration or differenceInCalendarMonths and create your own formatting function for your specific use case. Also, don't input your date string in that non-standard format. It will fail on Webkit (Safari) and Firefox, and create an Invalid Date. Good luck! |
Beta Was this translation helpful? Give feedback.
I'd recommend you leverage either intervalToDuration or differenceInCalendarMonths and create your own formatting function for your specific use case.
Also, don't input your date string in that non-standard format. It will fail on Webkit (Safari) and Firefox, and create an Invalid Date.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#syntax
https://en.wikipedia.org/wiki/ISO_8601
Good luck!