Time Functions in C#

CsharpProgrammingServer Side Programming

The DateTime has methods and properties for Date and Time as well like how to get the number of hours or minutes of a day, etc.

Let us only focus on the time functions −

Refer MSDN (Microsoft Developer Network) for all the functions −

Sr.No.Method & Properties
1AddDays(Double)
Returns a new DateTime that adds the specified number of days to the value of this instance.
2AddHours(Double)
Returns a new DateTime that adds the specified number of hours to the value of this instance.
3AddMilliseconds(Double)
Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.
4AddMinutes(Double)
Returns a new DateTime that adds the specified number of minutes to the value of this instance.
5AddSeconds(Double)
Returns a new DateTime that adds the specified number of seconds to the value of this instance.
6AddYears(Int32)
Returns a new DateTime that adds the specified number of years to the value of this instance.

Let us learn about a Time function i.e. AddMilliseconds(Double) to add the specified number of milliseconds to the value of this instance.

Example

 Live Demo

using System;

public class Demo {
   public static void Main() {
      string dateFormat = "MM/dd/yyyy hh:mm:ss.fffffff";
      DateTime dateCurrent = new DateTime(2018, 7, 23, 13, 0, 0);
      Console.WriteLine("Original date: {0} ({1:N0} ticks)\n", dateCurrent.ToString(dateFormat),          dateCurrent.Ticks);

      DateTime dateNew = dateCurrent.AddMilliseconds(1);
      Console.WriteLine("Next date: {0} ({1:N0} ticks)", dateNew.ToString(dateFormat),          dateNew.Ticks);

   }
}

Output

Original date: 07/23/2018 01:00:00.0000000 (636,679,476,000,000,000 ticks)
Next date: 07/23/2018 01:00:00.0010000 (636,679,476,000,010,000 ticks)
raja
Published on 09-Aug-2018 14:35:40
Advertisements