Date, time, duration, and age scenarios
Orientation
Choose the guide by the meaning of the value and the sentence you need:
| Task | Focused guide |
|---|---|
| Produce “yesterday” or “3 hours ago” | Relative dates and times |
| Produce “2 hours, 5 minutes” or an age phrase | Durations and ages |
Build a readable TimeSpan, DateTime, or DateOnly | Fluent dates and time spans |
Example
This verified source demonstrates the two most common display boundaries: a relative UTC date with an injected comparison instant and a two-unit elapsed duration:
using System.Globalization;
using Humanizer;
var culture = CultureInfo.GetCultureInfo("en-US");
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
var comparison = new DateTime(2025, 1, 20, 12, 0, 0, DateTimeKind.Utc);
var relative = comparison.AddDays(-1).Humanize(
utcDate: true,
dateToCompareAgainst: comparison,
culture: culture);
var duration = TimeSpan.FromMinutes(125).Humanize(precision: 2, culture: culture);
AssertEqual("yesterday", relative);
AssertEqual("2 hours, 5 minutes", duration);
Console.WriteLine($"{relative}; {duration}");
static void AssertEqual(string expected, string actual)
{
if (actual != expected)
throw new InvalidOperationException($"Expected '{expected}', got '{actual}'.");
}
It prints yesterday; 2 hours, 5 minutes. Follow the focused guides for the full overload contracts.
Pick a model before an API
- Use
DateTimeOffsetwhen an instant and offset matter. - Use
DateOnlyfor a calendar date without time-zone meaning. - Use
TimeOnlyfor a wall-clock time without a date. - Use
TimeSpanfor elapsed duration, not calendar arithmetic.
Pitfall
Reading Now inside formatting makes examples, tests, and cache behavior unstable. Inject comparison values and set culture explicitly. A TimeSpan has no calendar context, so months, years, and ages are approximations; use date arithmetic when leap years, month length, or birthdays must be exact.
Version notes
DateOnly and TimeOnly humanization are available on compatible target frameworks in 2.11.10. Clock notation and ToAge are unavailable.