Time Functions in C#

The DateTime structure in C# provides numerous methods and properties for working with dates and times. These time functions allow you to manipulate DateTime instances by adding or subtracting specific time intervals, extracting time components, and performing various time-related operations.

Time functions in C# are essential for applications that need to handle scheduling, logging, time calculations, and date arithmetic operations.

Common Time Functions

The following table shows the most commonly used time manipulation methods in the DateTime class −

Method Description
AddDays(Double) Returns a new DateTime that adds the specified number of days to the value of this instance.
AddHours(Double) Returns a new DateTime that adds the specified number of hours to the value of this instance.
AddMilliseconds(Double) Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.
AddMinutes(Double) Returns a new DateTime that adds the specified number of minutes to the value of this instance.
AddSeconds(Double) Returns a new DateTime that adds the specified number of seconds to the value of this instance.
AddYears(Int32) Returns a new DateTime that adds the specified number of years to the value of this instance.

Syntax

Following is the general syntax for DateTime Add methods −

DateTime newDate = originalDate.AddMethod(value);

Following is the syntax for accessing time properties −

int hours = dateTime.Hour;
int minutes = dateTime.Minute;
int seconds = dateTime.Second;

Using AddMilliseconds Method

The AddMilliseconds method adds a specified number of milliseconds to a DateTime instance and returns a new DateTime object −

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)
", dateCurrent.ToString(dateFormat), dateCurrent.Ticks); DateTime dateNew = dateCurrent.AddMilliseconds(1); Console.WriteLine("Next date: {0} ({1:N0} ticks)", dateNew.ToString(dateFormat), dateNew.Ticks); } }

The output of the above code is −

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)

Using Multiple Time Functions

You can combine multiple Add methods to perform complex time calculations −

using System;

public class TimeCalculations {
   public static void Main() {
      DateTime startTime = new DateTime(2023, 1, 15, 10, 30, 0);
      Console.WriteLine("Start time: " + startTime.ToString("yyyy-MM-dd HH:mm:ss"));
      
      DateTime newTime = startTime.AddDays(5).AddHours(3).AddMinutes(45);
      Console.WriteLine("After adding 5 days, 3 hours, 45 minutes: " + newTime.ToString("yyyy-MM-dd HH:mm:ss"));
      
      Console.WriteLine("Time difference: " + (newTime - startTime).TotalHours + " hours");
   }
}

The output of the above code is −

Start time: 2023-01-15 10:30:00
After adding 5 days, 3 hours, 45 minutes: 2023-01-20 14:15:00
Time difference: 123.75 hours

Extracting Time Components

DateTime provides properties to extract individual time components −

using System;

public class TimeComponents {
   public static void Main() {
      DateTime currentTime = DateTime.Now;
      Console.WriteLine("Current DateTime: " + currentTime);
      Console.WriteLine("Hour: " + currentTime.Hour);
      Console.WriteLine("Minute: " + currentTime.Minute);
      Console.WriteLine("Second: " + currentTime.Second);
      Console.WriteLine("Millisecond: " + currentTime.Millisecond);
      Console.WriteLine("Day of week: " + currentTime.DayOfWeek);
      Console.WriteLine("Day of year: " + currentTime.DayOfYear);
   }
}

The output will vary based on current system time, but follows this format −

Current DateTime: 11/15/2023 2:30:45 PM
Hour: 14
Minute: 30
Second: 45
Millisecond: 123
Day of week: Wednesday
Day of year: 319

Conclusion

Time functions in C# provide powerful capabilities for date and time manipulation through the DateTime class. The Add methods enable precise time calculations, while properties like Hour, Minute, and Second allow extraction of time components for various programming scenarios.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements