Skip to main content
Version: 4.0 (next)

Humanizer.TimeSpanHumanizeExtensions

TimeSpanHumanizeExtensions Class

Humanizes TimeSpan into human readable form

public static class TimeSpanHumanizeExtensions

Inheritance System.Object → TimeSpanHumanizeExtensions

Methods

TimeSpanHumanizeExtensions.Humanize(this TimeSpan, int, bool, CultureInfo, TimeUnit, TimeUnit, string, bool) Method

Turns a TimeSpan into a human readable form. E.g. 1 day.

public static string Humanize(this System.TimeSpan timeSpan, int precision, bool countEmptyUnits, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Week, Humanizer.TimeUnit minUnit=Humanizer.TimeUnit.Millisecond, string? collectionSeparator=", ", bool toWords=false);

Parameters

timeSpan System.TimeSpan

precision System.Int32

The maximum number of time units to return.

countEmptyUnits System.Boolean

Controls whether empty time units should be counted towards maximum number of time units. Leading empty time units never count.

culture System.Globalization.CultureInfo

Culture to use. If null, current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output. The default value is Week. The time units Month and Year will give approximations for time spans bigger than 30 days by calculating with 365.2425 days a year and 30.4369 days a month.

minUnit TimeUnit

The minimum unit of time to output.

collectionSeparator System.String

The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used.

toWords System.Boolean

Uses words instead of numbers if true. E.g. one day.

Returns

System.String

TimeSpanHumanizeExtensions.Humanize(this TimeSpan, int, CultureInfo, TimeUnit, TimeUnit, string, bool) Method

Turns a TimeSpan into a human readable form. E.g. 1 day.

public static string Humanize(this System.TimeSpan timeSpan, int precision=1, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Week, Humanizer.TimeUnit minUnit=Humanizer.TimeUnit.Millisecond, string? collectionSeparator=", ", bool toWords=false);

Parameters

timeSpan System.TimeSpan

precision System.Int32

The maximum number of time units to return. Defaulted is 1 which means the largest unit is returned

culture System.Globalization.CultureInfo

Culture to use. If null, current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output. The default value is Week. The time units Month and Year will give approximations for time spans bigger 30 days by calculating with 365.2425 days a year and 30.4369 days a month.

minUnit TimeUnit

The minimum unit of time to output.

collectionSeparator System.String

The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used.

toWords System.Boolean

Uses words instead of numbers if true. E.g. one day.

Returns

System.String

TimeSpanHumanizeExtensions.HumanizeToSymbols(this TimeSpan, int, bool, CultureInfo, TimeUnit, TimeUnit, string) Method

Turns a System.TimeSpan into a human readable form using localized unit symbols.

public static string HumanizeToSymbols(this System.TimeSpan timeSpan, int precision, bool countEmptyUnits, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Week, Humanizer.TimeUnit minUnit=Humanizer.TimeUnit.Millisecond, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

precision System.Int32

The maximum number of time units to return.

countEmptyUnits System.Boolean

Controls whether empty time units should be counted towards the maximum number of time units. Leading empty time units never count.

culture System.Globalization.CultureInfo

Culture to use. If null, current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output. The default value is Week.

minUnit TimeUnit

The minimum unit of time to output.

collectionSeparator System.String

The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used.

Returns

System.String

TimeSpanHumanizeExtensions.HumanizeToSymbols(this TimeSpan, int, CultureInfo, TimeUnit, TimeUnit, string) Method

Turns a System.TimeSpan into a human readable form using localized unit symbols.

public static string HumanizeToSymbols(this System.TimeSpan timeSpan, int precision=1, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Week, Humanizer.TimeUnit minUnit=Humanizer.TimeUnit.Millisecond, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

precision System.Int32

The maximum number of time units to return.

culture System.Globalization.CultureInfo

Culture to use. If null, current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output. The default value is Week.

minUnit TimeUnit

The minimum unit of time to output.

collectionSeparator System.String

The separator to use when combining humanized time parts. If null, the default collection formatter for the current culture is used.

Returns

System.String

TimeSpanHumanizeExtensions.HumanizeToSymbolsWithFractionalSeconds(this TimeSpan, int, bool, int, MidpointRounding, CultureInfo, TimeUnit, string) Method

Turns a System.TimeSpan into a human-readable form using localized unit symbols and fractional seconds.

public static string HumanizeToSymbolsWithFractionalSeconds(this System.TimeSpan timeSpan, int precision, bool countEmptyUnits, int maxFractionalDigits, System.MidpointRounding roundingMode, System.Globalization.CultureInfo? culture, Humanizer.TimeUnit maxUnit, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

precision System.Int32

The maximum number of time units to return.

countEmptyUnits System.Boolean

Whether empty time units count toward precision.

maxFractionalDigits System.Int32

The maximum number of fractional-second digits, from 0 through 7.

roundingMode System.MidpointRounding

The midpoint rounding mode. Only System.MidpointRounding.ToEven and System.MidpointRounding.AwayFromZero are supported.

culture System.Globalization.CultureInfo

Culture to use. If null, the current culture is used.

maxUnit TimeUnit

The maximum unit of time to output.

collectionSeparator System.String

The separator used to combine time parts. If null, the culture's default collection formatter is used.

Returns

System.String
The human-readable time span, using seconds as its minimum unit.

TimeSpanHumanizeExtensions.HumanizeToSymbolsWithFractionalSeconds(this TimeSpan, int, int, MidpointRounding, CultureInfo, TimeUnit, string) Method

Turns a System.TimeSpan into a human-readable form using localized unit symbols and fractional seconds.

public static string HumanizeToSymbolsWithFractionalSeconds(this System.TimeSpan timeSpan, int precision, int maxFractionalDigits, System.MidpointRounding roundingMode, System.Globalization.CultureInfo? culture, Humanizer.TimeUnit maxUnit, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

precision System.Int32

The maximum number of time units to return.

maxFractionalDigits System.Int32

The maximum number of fractional-second digits, from 0 through 7.

roundingMode System.MidpointRounding

The midpoint rounding mode. Only System.MidpointRounding.ToEven and System.MidpointRounding.AwayFromZero are supported.

culture System.Globalization.CultureInfo

Culture to use. If null, the current culture is used.

maxUnit TimeUnit

The maximum unit of time to output.

collectionSeparator System.String

The separator used to combine time parts. If null, the culture's default collection formatter is used.

Returns

System.String
The human-readable time span, using seconds as its minimum unit.

TimeSpanHumanizeExtensions.HumanizeWithCase(this TimeSpan, GrammaticalCase, int, bool, CultureInfo, TimeUnit, TimeUnit, string) Method

Turns a System.TimeSpan into a bare duration using locale-authored unit-case phrases. No preposition is added.

public static string HumanizeWithCase(this System.TimeSpan timeSpan, Humanizer.GrammaticalCase grammaticalCase, int precision, bool countEmptyUnits, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Week, Humanizer.TimeUnit minUnit=Humanizer.TimeUnit.Millisecond, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

grammaticalCase GrammaticalCase

The grammatical case used to select each unit phrase.

precision System.Int32

The maximum number of time units to return.

countEmptyUnits System.Boolean

Whether empty time units count toward precision.

culture System.Globalization.CultureInfo

Culture to use. If null, the current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output.

minUnit TimeUnit

The minimum unit of time to output.

collectionSeparator System.String

The separator used to combine time parts. If null, the culture's default collection formatter is used.

Returns

System.String
The locale-authored unit-case phrase. The count may be written explicitly or encoded by the unit form.

Exceptions

System.ArgumentOutOfRangeException
grammaticalCase is not defined.

System.NotSupportedException
The configured strategy, selected formatter, locale, or duration unit does not support the requested case.

TimeSpanHumanizeExtensions.HumanizeWithCase(this TimeSpan, GrammaticalCase, int, CultureInfo, TimeUnit, TimeUnit, string) Method

Turns a System.TimeSpan into a bare duration using locale-authored unit-case phrases. No preposition is added.

public static string HumanizeWithCase(this System.TimeSpan timeSpan, Humanizer.GrammaticalCase grammaticalCase, int precision=1, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Week, Humanizer.TimeUnit minUnit=Humanizer.TimeUnit.Millisecond, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

grammaticalCase GrammaticalCase

The grammatical case used to select each unit phrase.

precision System.Int32

The maximum number of time units to return.

culture System.Globalization.CultureInfo

Culture to use. If null, the current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output.

minUnit TimeUnit

The minimum unit of time to output.

collectionSeparator System.String

The separator used to combine time parts. If null, the culture's default collection formatter is used.

Returns

System.String
The locale-authored unit-case phrase. The count may be written explicitly or encoded by the unit form.

Exceptions

System.ArgumentOutOfRangeException
grammaticalCase is not defined.

System.NotSupportedException
The configured strategy, selected formatter, locale, or duration unit does not support the requested case.

TimeSpanHumanizeExtensions.HumanizeWithFractionalSeconds(this TimeSpan, int, bool, int, MidpointRounding, CultureInfo, TimeUnit, string) Method

Turns a System.TimeSpan into a human-readable form with fractional seconds.

public static string HumanizeWithFractionalSeconds(this System.TimeSpan timeSpan, int precision, bool countEmptyUnits, int maxFractionalDigits, System.MidpointRounding roundingMode, System.Globalization.CultureInfo? culture, Humanizer.TimeUnit maxUnit, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

precision System.Int32

The maximum number of time units to return.

countEmptyUnits System.Boolean

Whether empty time units count toward precision.

maxFractionalDigits System.Int32

The maximum number of fractional-second digits, from 0 through 7.

roundingMode System.MidpointRounding

The midpoint rounding mode. Only System.MidpointRounding.ToEven and System.MidpointRounding.AwayFromZero are supported.

culture System.Globalization.CultureInfo

Culture to use. If null, the current culture is used.

maxUnit TimeUnit

The maximum unit of time to output.

collectionSeparator System.String

The separator used to combine time parts. If null, the culture's default collection formatter is used.

Returns

System.String
The human-readable time span, using seconds as its minimum unit.

TimeSpanHumanizeExtensions.HumanizeWithFractionalSeconds(this TimeSpan, int, int, MidpointRounding, CultureInfo, TimeUnit, string) Method

Turns a System.TimeSpan into a human-readable form with fractional seconds.

public static string HumanizeWithFractionalSeconds(this System.TimeSpan timeSpan, int precision, int maxFractionalDigits, System.MidpointRounding roundingMode, System.Globalization.CultureInfo? culture, Humanizer.TimeUnit maxUnit, string? collectionSeparator=", ");

Parameters

timeSpan System.TimeSpan

The time span to humanize.

precision System.Int32

The maximum number of time units to return.

maxFractionalDigits System.Int32

The maximum number of fractional-second digits, from 0 through 7.

roundingMode System.MidpointRounding

The midpoint rounding mode. Only System.MidpointRounding.ToEven and System.MidpointRounding.AwayFromZero are supported.

culture System.Globalization.CultureInfo

Culture to use. If null, the current culture is used.

maxUnit TimeUnit

The maximum unit of time to output.

collectionSeparator System.String

The separator used to combine time parts. If null, the culture's default collection formatter is used.

Returns

System.String
The human-readable time span, using seconds as its minimum unit.

TimeSpanHumanizeExtensions.ToAge(this TimeSpan, CultureInfo, TimeUnit, bool) Method

Turns a TimeSpan into an age expression, e.g. "40 years old"

public static string ToAge(this System.TimeSpan timeSpan, System.Globalization.CultureInfo? culture=null, Humanizer.TimeUnit maxUnit=Humanizer.TimeUnit.Year, bool toWords=false);

Parameters

timeSpan System.TimeSpan

Elapsed time

culture System.Globalization.CultureInfo

Culture to use. If null, current thread's culture is used.

maxUnit TimeUnit

The maximum unit of time to output. The default value is Year.

toWords System.Boolean

Uses words instead of numbers if true. E.g. "forty years old".

Returns

System.String
Age expression in the given culture/language