6

I'm using Humanizer to format a TimeSpan object. It's very handy, however, I also want my string to be formatted like so: "xd yh zm as" instead of "x days, y hours, z minutes, a seconds". I'm currently just replacing those substrings after Humanize does its thing, but it feels very hacky.

TimeSpan difference = DateTime.Now - myTime;

_myString = difference.Humanize(3, maxUnit:Humanizer.Localisation.TimeUnit.Day, minUnit:Humanizer.Localisation.TimeUnit.Second);
_myString = _myString.Replace(" hours", "h").Replace(" hour", "h").Replace(" minutes", "m").Replace(" minute", "m").Replace(" days", "d").Replace(" day", "d").Replace(" seconds", "s").Replace(" second", "s").Replace(",", "");

Is there a better way of doing this? I'm aware of a few questions concerning how to format TimeSpans, but I'm specifically looking for a neater way to format when using Humanizer.

Alistair
  • 1,104
  • 9
  • 15

0 Answers0