Class DateTimeExtensions
Provides extension methods for System.DateTime.
Inheritance
System.Object
DateTimeExtensions
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Sartorius.SAF.Extensions
Assembly: Sartorius.SAF.dll
Syntax
public static class DateTimeExtensions
Methods
View SourceRoundToPrecision(DateTime, Int32)
Rounds to the given count of decimals. e.g. 2 will round to 1/100th second.
Declaration
public static DateTime RoundToPrecision(this DateTime value, int decimals)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | value | The value. |
System.Int32 | decimals | The decimals. |
Returns
Type | Description |
---|---|
System.DateTime |
Examples
The following example rounds a datetime to 1/100th (2 decimals) of a second.
var expected = new DateTime(2015, 9, 11, 14, 38, 12, 990);
var time = new DateTime(2015, 9, 11, 14, 38, 12, 994).RoundToPrecision(2);
View Source
RoundToTicks(DateTime, Int64)
Rounds to the specified ticks.
Declaration
public static DateTime RoundToTicks(this DateTime value, long precision)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | value | The value. |
System.Int64 | precision | The tick precision. |
Returns
Type | Description |
---|---|
System.DateTime |
Examples
The following example rounds a datetime to full days by using System.TimeSpan.TicksPerDay.
var expected = new DateTime(2015, 9, 12, 0, 0, 0, 0);
var time = new DateTime(2015, 9, 11, 14, 38, 12, 994).RoundToTicks(TimeSpan.TicksPerDay);
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException | precision;precisions must be greater than 0. |