Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.AddMinutes() Method in C#
The DateTime.AddMinutes() method in C# is used to add the specified number of minutes to the value of a DateTime instance. It returns a new DateTime object representing the calculated time without modifying the original instance.
Syntax
Following is the syntax −
public DateTime AddMinutes(double minutes);
Parameters
minutes − A double value representing the number of minutes to add. The value can be positive (to add minutes) or negative (to subtract minutes). Fractional values are allowed.
Return Value
Returns a new DateTime object whose value is the sum of the date and time represented by this instance and the number of minutes represented by the minutes parameter.
Using AddMinutes() to Add Minutes
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25);
DateTime d2 = d1.AddMinutes(25);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime (After adding minutes) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 10 September 2019, 05:15:25 New DateTime (After adding minutes) = 10 September 2019, 05:40:25
Using AddMinutes() to Subtract Minutes
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25);
DateTime d2 = d1.AddMinutes(-15);
Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime (After subtracting minutes) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 10 September 2019, 05:15:25 New DateTime (After subtracting minutes) = 10 September 2019, 05:00:25
Using Fractional Minutes
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25);
DateTime d2 = d1.AddMinutes(1.5); // Add 1 minute and 30 seconds
Console.WriteLine("Initial DateTime = {0:HH:mm:ss}", d1);
Console.WriteLine("After adding 1.5 minutes = {0:HH:mm:ss}", d2);
}
}
The output of the above code is −
Initial DateTime = 05:15:25 After adding 1.5 minutes = 05:16:55
Conclusion
The DateTime.AddMinutes() method provides a convenient way to add or subtract minutes from a DateTime instance. It accepts both positive and negative values, including fractional minutes, and returns a new DateTime object while preserving the original instance.
