[Update]: Since this post Humanizer has gone a very long way. It has also been featured on Scott Hanselman’s blog on his ‘NuGet of the week’ series. Please check out the project homepage on GitHub to see the latest.

This post is about the features added in V0.5 and some of the other changes. If you don’t know Humanizer you might want to read this intro and Humanizer V0.4’s release notes.

Humanizer V0.5 provides a fluent API to deal with DateTime and TimeSpan as follows:

TimeSpan methods:

2.Milliseconds() // Returns new TimeSpan(0, 0, 0, 0, 2)
2.Seconds() // Returns new TimeSpan(0, 0, 0, 2)
2.Minutes() // Returns new TimeSpan(0, 0, 2, 0)
2.Hours() // Returns new TimeSpan(0, 2, 0, 0)
2.Days() // Returns new TimeSpan(2, 0, 0, 0)
2.Weeks() // Returns new TimeSpan(14, 0, 0, 0)

There are no fluent APIs for month or year as a month could have between 28 to 31 days and a year could be 365 or 366 days.

You could use these methods to, for example, replace

DateTime.Now.AddDays(2).AddHours(3).AddMinutes(-5)

with

DateTime.Now + 2.Days() + 3.Hours() - 5.Minutes()

There are also fluent methods to deal with DateTime:

In.TheYear(2010) // Returns the first of January of 2010
In.January // Returns 1st of January of the current year
In.FebruaryOf(2009) // Returns 1st of February of 2009
In.One.Second //  DateTime.UtcNow.AddSeconds(1);
In.Two.SecondsFrom(DateTime dateTime)
In.Three.Minutes // With corresponding From method
In.Three.Hours // With corresponding From method
In.Three.Days // With corresponding From method
In.Three.Weeks // With corresponding From method
In.Three.Months // With corresponding From method
In.Three.Years // With corresponding From method
On.January.The4th // Returns 4th of January of the current year
On.February.The(12) // Returns 12th of Feb of the current year

and some extension methods:

var someDateTime = new DateTime(2011, 2, 10, 5, 25, 45, 125);
someDateTime.In(2008) // Returns new DateTime(2008, 2, 10, 5, 25, 45, 125) changing the year to 2008
someDateTime.At(2) // Returns new DateTime(2011, 2, 10, 2, 25, 45, 125) changing the hour to 2:25:45.125
someDateTime.At(2, 20, 15) // Returns new DateTime(2011, 2, 10, 2, 20, 15, 125) changing the time to 2:20:15.125
someDateTime.AtNoon() // Returns new DateTime(2011, 2, 10, 12, 0, 0) changing the time to 12:00:00.000
someDateTime.AtMidnight() // Returns new DateTime(2011, 2, 10, 0, 0, 0) changing the time to 00:00:00.000

Obviously you could chain the methods too; e.g. On.November.The13th.In(2010).AtNoon + 5.Minutes()

In this release there is also a cool new ToWords extension method which changes numbers to words. Here is a few examples from the unit tests:

Assert.Equal("one", 1.ToWords());
Assert.Equal("ten", 10.ToWords());
Assert.Equal("eleven", 11.ToWords());
Assert.Equal("one hundred and twenty-two", 122.ToWords());
Assert.Equal("three thousand five hundred and one", 3501.ToWords());

In this release we also get Romanian localization for DateTime Humanize. As a side note, as the number of supported locales increased I also changed the nuget package to instead of embedding the localization resources to include them in the nuget package properly; so now the package looks like:

Localized Humanizer nuget

If this is the first time you see Humanizer you should check out the readme on github to see the full API.