Avatar ·

How to calculate relative time in C# (like "5 minutes ago", "yesterday")?

Hi everyone! I'm writing a small C# (Windows Forms) application for event logging. I need to display the date and time of each event not in absolute format (e.g., "2023-10-05 14:30:00"), but in relative format — like in social networks or messengers: "just now", "5 minutes ago", "2 hours ago", "yesterday", "3 days ago", etc. I'm new to C# and so far I can only think of solving this with a bunch of if-else statements, but it looks terribly cumbersome. For example, I have a `DateTime eventTime` variable, and I want to get a string like "15 minutes ago". Here's my current "code" (or rather, a draft) that I tried to write: ```csharp DateTime now = DateTime.Now; TimeSpan diff = now - eventTime; if (diff.TotalMinutes < 1) return "just now"; else if (diff.TotalMinutes < 60) ```