如何在TimeSpan对象上使用Humanize时自定义返回的格式化字符串?

时间:2017-02-28 16:31:10

标签: c# .net humanizer

我正在使用Humanizer格式化TimeSpan对象。这非常方便,但是,我也希望我的字符串格式如下:“xd yh zm as”而不是“x days,y hours,z minutes,a seconds”。我现在只是在Humanize做了它之后才更换那些子串,但感觉非常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(",", "");

有更好的方法吗?我知道有关如何格式化TimeSpans的一些问题,但我特别想找到一种更简洁的格式化使用Humanizer的方法。

0 个答案:

没有答案