Home » C#

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#.
Submitted by IncludeHelp, on October 22, 2019

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.