C# | DateTime.AddTicks() Method with Example

DateTime.AddTicks() Method: Here, we are going to learn about the AddTicks() method of DateTime class with example in C#. By IncludeHelp Last updated : August 27, 2023

C# DateTime.AddTicks() Method

DateTime.AddTicks() method is used to return a new date-time object that adds ticks value of this instance. This object does not change the original value of date-time but it returns an object with the new value.

Syntax

DateTime DateTime.AddTicks(long value);

Parameter(s)

  • long value – represents a long value to be added in the DateTime object.

Return value

The return type of this method is DateTime, it returns the new value of DateTime object.

Example to demonstrate example of DateTime.AddTicks() method

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create object of datetime class
            DateTime dt1 = new DateTime(2019,1,1,5,0,15);

            //Adding 5000 ticks using AddTicks method
            DateTime dt2 = dt1.AddTicks(5000);

            //Display ticks of dt1
            Console.WriteLine("Number of ticks in dt1 : " + dt1.Ticks);

            //Display ticks of dt2
            Console.WriteLine("Number of ticks in dt2 : " + dt2.Ticks);

            Console.WriteLine();  
        }
    }
}

Output

Number of ticks in dt1 : 636819156150000000
Number of ticks in dt2 : 636819156150005000

In the above program, we took the dt1 object of the DateTime class and then add ticks using AddTicks() method and assign an object with new values into dt2. Then print ticks values using Ticks property of DateTime class.


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.