Skip to main content
Version: 4.0 (next)

Humanizer.NumberToTimeSpanExtensions

NumberToTimeSpanExtensions Class

Provides extension methods for converting numeric values to System.TimeSpan instances, enabling fluent and readable time duration creation (e.g., 5.Seconds(), 3.Hours(), 2.Weeks()).

public static class NumberToTimeSpanExtensions

Inheritance System.Object → NumberToTimeSpanExtensions

Remarks

These extensions make it easy to create TimeSpan values in a more natural, readable way: - Instead of TimeSpan.FromHours(3), you can write 3.Hours() - Instead of TimeSpan.FromMinutes(30), you can write 30.Minutes() - Supports all numeric types: byte, sbyte, short, ushort, int, uint, long, ulong, and double - Weeks are converted to days (1 week = 7 days)

Methods

NumberToTimeSpanExtensions.Days(this byte) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this byte days);

Parameters

days System.Byte

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

((byte)2).Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this double) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this double days);

Parameters

days System.Double

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

2.Days() => TimeSpan representing 2 days
7.Days() => TimeSpan representing 1 week
1.5.Days() => TimeSpan representing 1 day and 12 hours

NumberToTimeSpanExtensions.Days(this int) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this int days);

Parameters

days System.Int32

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

2.Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this long) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this long days);

Parameters

days System.Int64

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

2L.Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this sbyte) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this sbyte days);

Parameters

days System.SByte

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

((sbyte)2).Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this short) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this short days);

Parameters

days System.Int16

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

((short)2).Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this uint) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this uint days);

Parameters

days System.UInt32

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

2U.Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this ulong) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this ulong days);

Parameters

days System.UInt64

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

2UL.Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Days(this ushort) Method

Creates a System.TimeSpan representing the specified number of days.

public static System.TimeSpan Days(this ushort days);

Parameters

days System.UInt16

The number of days.

Returns

System.TimeSpan
A System.TimeSpan representing days days.

Example

((ushort)2).Days() => TimeSpan.FromDays(2)

NumberToTimeSpanExtensions.Hours(this byte) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this byte hours);

Parameters

hours System.Byte

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

((byte)3).Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this double) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this double hours);

Parameters

hours System.Double

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

3.Hours() => TimeSpan representing 3 hours
24.Hours() => TimeSpan representing 1 day
1.5.Hours() => TimeSpan representing 1 hour and 30 minutes

NumberToTimeSpanExtensions.Hours(this int) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this int hours);

Parameters

hours System.Int32

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

3.Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this long) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this long hours);

Parameters

hours System.Int64

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

3L.Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this sbyte) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this sbyte hours);

Parameters

hours System.SByte

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

((sbyte)3).Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this short) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this short hours);

Parameters

hours System.Int16

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

((short)3).Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this uint) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this uint hours);

Parameters

hours System.UInt32

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

3U.Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this ulong) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this ulong hours);

Parameters

hours System.UInt64

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

3UL.Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Hours(this ushort) Method

Creates a System.TimeSpan representing the specified number of hours.

public static System.TimeSpan Hours(this ushort hours);

Parameters

hours System.UInt16

The number of hours.

Returns

System.TimeSpan
A System.TimeSpan representing hours hours.

Example

((ushort)3).Hours() => TimeSpan.FromHours(3)

NumberToTimeSpanExtensions.Milliseconds(this byte) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this byte ms);

Parameters

ms System.Byte

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

500.Milliseconds() => TimeSpan representing 500 milliseconds
1000.Milliseconds() => TimeSpan representing 1 second

NumberToTimeSpanExtensions.Milliseconds(this double) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this double ms);

Parameters

ms System.Double

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

500.0.Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this int) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this int ms);

Parameters

ms System.Int32

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

500.Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this long) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this long ms);

Parameters

ms System.Int64

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

500L.Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this sbyte) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this sbyte ms);

Parameters

ms System.SByte

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

((sbyte)500).Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this short) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this short ms);

Parameters

ms System.Int16

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

((short)500).Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this uint) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this uint ms);

Parameters

ms System.UInt32

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

500U.Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this ulong) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this ulong ms);

Parameters

ms System.UInt64

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

500UL.Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Milliseconds(this ushort) Method

Creates a System.TimeSpan representing the specified number of milliseconds.

public static System.TimeSpan Milliseconds(this ushort ms);

Parameters

ms System.UInt16

The number of milliseconds.

Returns

System.TimeSpan
A System.TimeSpan representing ms milliseconds.

Example

((ushort)500).Milliseconds() => TimeSpan.FromMilliseconds(500)

NumberToTimeSpanExtensions.Minutes(this byte) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this byte minutes);

Parameters

minutes System.Byte

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

((byte)30).Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this double) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this double minutes);

Parameters

minutes System.Double

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

30.Minutes() => TimeSpan representing 30 minutes
90.Minutes() => TimeSpan representing 1 hour and 30 minutes

NumberToTimeSpanExtensions.Minutes(this int) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this int minutes);

Parameters

minutes System.Int32

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

30.Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this long) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this long minutes);

Parameters

minutes System.Int64

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

30L.Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this sbyte) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this sbyte minutes);

Parameters

minutes System.SByte

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

((sbyte)30).Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this short) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this short minutes);

Parameters

minutes System.Int16

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

((short)30).Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this uint) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this uint minutes);

Parameters

minutes System.UInt32

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

30U.Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this ulong) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this ulong minutes);

Parameters

minutes System.UInt64

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

30UL.Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Minutes(this ushort) Method

Creates a System.TimeSpan representing the specified number of minutes.

public static System.TimeSpan Minutes(this ushort minutes);

Parameters

minutes System.UInt16

The number of minutes.

Returns

System.TimeSpan
A System.TimeSpan representing minutes minutes.

Example

((ushort)30).Minutes() => TimeSpan.FromMinutes(30)

NumberToTimeSpanExtensions.Seconds(this byte) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this byte seconds);

Parameters

seconds System.Byte

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

((byte)30).Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this double) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this double seconds);

Parameters

seconds System.Double

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

30.Seconds() => TimeSpan representing 30 seconds
90.Seconds() => TimeSpan representing 1 minute and 30 seconds

NumberToTimeSpanExtensions.Seconds(this int) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this int seconds);

Parameters

seconds System.Int32

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

30.Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this long) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this long seconds);

Parameters

seconds System.Int64

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

30L.Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this sbyte) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this sbyte seconds);

Parameters

seconds System.SByte

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

((sbyte)30).Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this short) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this short seconds);

Parameters

seconds System.Int16

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

((short)30).Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this uint) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this uint seconds);

Parameters

seconds System.UInt32

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

30U.Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this ulong) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this ulong seconds);

Parameters

seconds System.UInt64

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

30UL.Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Seconds(this ushort) Method

Creates a System.TimeSpan representing the specified number of seconds.

public static System.TimeSpan Seconds(this ushort seconds);

Parameters

seconds System.UInt16

The number of seconds.

Returns

System.TimeSpan
A System.TimeSpan representing seconds seconds.

Example

((ushort)30).Seconds() => TimeSpan.FromSeconds(30)

NumberToTimeSpanExtensions.Weeks(this byte) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this byte input);

Parameters

input System.Byte

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

((byte)2).Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this double) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this double input);

Parameters

input System.Double

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

2.Weeks() => TimeSpan representing 14 days
1.Weeks() => TimeSpan representing 7 days
0.5.Weeks() => TimeSpan representing 3.5 days

Remarks

Since System.TimeSpan doesn't have a native concept of weeks, this method converts weeks to days (multiplying by 7).

NumberToTimeSpanExtensions.Weeks(this int) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this int input);

Parameters

input System.Int32

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

2.Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this long) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this long input);

Parameters

input System.Int64

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

2L.Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this sbyte) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this sbyte input);

Parameters

input System.SByte

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

((sbyte)2).Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this short) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this short input);

Parameters

input System.Int16

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

((short)2).Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this uint) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this uint input);

Parameters

input System.UInt32

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

2U.Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this ulong) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this ulong input);

Parameters

input System.UInt64

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

2UL.Weeks() => new TimeSpan(14, 0, 0, 0)

NumberToTimeSpanExtensions.Weeks(this ushort) Method

Creates a System.TimeSpan representing the specified number of weeks.

public static System.TimeSpan Weeks(this ushort input);

Parameters

input System.UInt16

The number of weeks.

Returns

System.TimeSpan
A System.TimeSpan representing input weeks (converted to days: 1 week = 7 days).

Example

((ushort)2).Weeks() => new TimeSpan(14, 0, 0, 0)