I have following code for AddTicks
method. Ticks property of datetime object is returning same value before and after the AddTick method. Why is it behaving so?
There are 10,000 ticks in a millisecond.
Ticks: The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue.
AddTicks : Adds the specified number of ticks to the value of this instance.
Note: I am using .Net 4.0
framework
CODE
static void Main()
{
DateTime dt2 = new DateTime(2010, 5, 7, 10, 11, 12, 222);
long x = dt2.Ticks;
dt2.AddTicks(9999);
long y = dt2.Ticks;
bool isSame = false;
if (x == y)
{
isSame = true;
}
Console.WriteLine(isSame);
System.Console.ReadKey();
}
AddTicks() method in C# is used to add a specified number of ticks to the value of this instance. It returns a new DateTime.
AddTicks() Method in C# This method is used to returns a new DateTime that adds the specified number of ticks to the value of this instance. This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation.
If the DateTime object has its Kind property set to Unspecified , its ticks represent the time elapsed time since 12:00:00 midnight, January 1, 0001 in the unknown time zone. In general, the ticks represent the time according to the time zone specified by the Kind property.
AddTicks
(and the other Add*
methods) does not alter the DateTime, but returns a new object.
So you should use
dt2 = dt2.AddTicks(...)
DateTime
is a value type and is immutable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With